0

Why does this return 23 rows (the right amount):

select users.user_id, users.fname, users.lname, 
       stars.stars, comments.comment from users 
       LEFT JOIN stars on users.user_id = stars.userid 
       JOIN comments on users.user_id = comments.sender 
       where users.user_id = ? order by comments.time desc;

and this return 1 row?:

select users.user_id, users.fname, users.lname,
       stars.stars, count(distinct comments.id) as amount,
       comments.comment from users 
       LEFT JOIN stars on users.user_id = stars.userid 
       JOIN comments on users.user_id = comments.sender 
       where users.user_id = ? order by comments.time desc;

Cheers.

1
  • it would be best to show some sample data? Commented Apr 3, 2012 at 1:09

1 Answer 1

3

you need to group the main data or do a sub-query for the field.

Sign up to request clarification or add additional context in comments.

4 Comments

@AmitBhargava well if i put group by comments.id at the end it returns the right amount of rows but the count() doesnt so i dont know
@AndyLobel what is it you are trying to select?
@Alex hey, I know this is old now but i still cant do it looool, well I can do it with the subquery but not the group by which is what i want. What would i actually tell it to group it by? Cheeeeeers
Reverse your query. SELECT [columns], [count] FROM comments JOIN users ON [condition] WHERE [condition] GROUP BY comments.sender - this will give your proper count of comments by users

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.