Quote:
Originally Posted by Invisionary
things
- ID
- name
- rating
- 6.5 5.0 4.5
- 2.5 4.0 8.0
- 9.0 1.5 7.5
- rated_by
Example:
user "ID" 1 gave thing "ID" 2 a "rating" of 8.0
|
Quote:
Originally Posted by Invisionary
Is the only way to do this is to keep that special information (obtained and/or calculated from existing info) in it's own table column, or is there a better way to construct the two tables? If so, what's the better method?
|
A much better solution in my opinion would be to create a third table, such as thing_ratings with fields: id, thing_id, rated_by, and rating. This way each rating has its own row. You won't have to mess with delimiters or serialization, which should make for cleaner code, as well as likely improve performance.
Quote:
Originally Posted by Invisionary
This wouldn't normally be a problem except that I want to do things like take the average rating of a particular thing on the fly and more importantly sort the things table by highest average rating. Likewise, I want to be able to able to do a mySQL command that selects all the user "names" that rated a particular thing.
|
Using the third table, the queries would be on the simpler side compared to dealing with delimiters or unserializing data. I wrote a few below, but I didn't test them.
Average would be something like:
SELECT thing_id, AVG(rating) FROM thing_ratings GROUP BY thing_id
Order by average:
SELECT thing_id, AVG(rating) as rate FROM thing_ratings GROUP BY thing_id ORDER BY rate DESC
Users who rated an item (where $id is the id of the item)
SELECT users.name FROM thing_ratings JOIN users ON things_ratings.rated_by=users.id WHERE thing_id=$id
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
Last edited by frost; 12-15-2007 at 10:45 PM..
|