tags:

views:

43

answers:

2

There is a query I've just made

    SELECT * 
      FROM users 
INNER JOIN ratings ON ratings.rateable=users.id 
  ORDER BY SUM(ratings.rating)/COUNT(ratings.rating)

But, it doesn't work, I just get one person in result, although there are 3 people in ratings table! I'm using php 5.

I think sum(), count() doesn't work at all! Please, help!! Cause I can't understand how to build TOP RATED system.

A: 

It's hard to tell because I can't see all of your stuff....but my guess is in the ' ON ratings.rateable=users.id ' part of your SQL. Are you storing the users.id field value in the rateable column in your ratings table?

SOA Nerd
yes, I do what you're saying
Anton
+1  A: 

This is just a hunch, but ratings.rateable smells like a bit to me. Any chance the one result has users.id 1?

Is ratings.rateable really the foreign key to users.id?

Jay
yes, I've checked, ratings.rateable really the foreign key to users.id
Anton
Take out the `ORDER BY` clause to isolate the problem.
Jay
Are the 3 people in the ratings table unique? That is, does each of the 3 records in the ratings table have a different value in the `rateable` column?
Jay
I get 5 people avatars. At all There are 3 people in ratings table, but 2 of them have 2 ratings, so I get 5 people avatars
Anton
No, There are five records in table ratings, one record is unique
Anton
5 records, 1 unique. I don't understand why in the question it says "there are 3 people in ratings table!"
Jay
hm, 3 different users
Anton
What are the 5 values for the `rateable` column in the `ratings` table?
Jay
23113End of rateable column
Anton
Did you try the query without `ORDER BY`?
Jay
yesI got 5 photos of users
Anton
Clearly then your `ORDER BY` clause is the problem, which, now that I look at it, should have been obvious all along. I assume you are trying to order by average rating?
Jay
yes, by average rating
Anton
Let's start by wrapping the Sum/Count clause in parentheses.
Jay