开发者

SQL Distinct Question [closed]

开发者 https://www.devze.com 2023-03-21 04:21 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. 开发者_如何学编程
Closed. This question needs details or clarity. It is not currently accepting answers.
开发者_如何学编程

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

So 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 IDs 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
0

精彩评论

暂无评论...
验证码 换一张
取 消