开发者_如何学编程
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this questionSo I'm tired, but why isn't DISTINCT working here:
SELECT DISTINCT Review.user_id, Review.id, User.*, Account.*
FROM reviews As Review
INNER JOIN users AS User ON Review.user_id = User.id
LEFT JOIN accounts AS Account ON User.id = Account.user_id
Since the Unique only works on the result which are exactly same. But here you are including ID
s as well, which will be unique for each row making each record unique. So the result is showing all the records.
If you want only unique user_id's, try GROUP'ing by it. But this will truncate all other joined records from the result. So you better think what do you want to select with user_id and what you don't.
SELECT DISTINCT Review.user_id, Review.id, User.*, Account.*
FROM reviews As Review
INNER JOIN users AS User ON Review.user_id = User.id
LEFT JOIN accounts AS Account ON User.id = Account.user_id
GROUP BY Review.user_id
精彩评论