SQL 独特问题

发布于 2024-11-24 16:11:33 字数 1432 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑕疵 2024-12-01 16:11:33

由于“唯一”仅适用于完全相同的结果。但在这里您还包括 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.

痞味浪人 2024-12-01 16:11:33

如果您只想要唯一的 user_id,请尝试按它进行分组。但这将从结果中截断所有其他连接的记录。所以你最好想想你想用 user_id 选择什么,不想选择什么。

  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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文