简体   繁体   English

SQL查询未返回唯一结果。 我需要使用哪种类型的联接?

[英]SQL query not returning unique results. Which type of join do I need to use?

I'm trying to run the following MySQL query: 我正在尝试运行以下MySQL查询:

SELECT * 
FROM user u 
JOIN user_categories uc ON u.user_id = uc.user_id 
WHERE (uc.category_id = 3 OR uc.category_id = 1)

It currently returns: 当前返回:

Joe,Smith,60657,male
Joe,Smith,60657,male
Mickey,Mouse,60613,female
Petter,Pan,60625,male
Petter,Pan,60625,male
Donald,Duck,60615,male

If the user belongs to both categories it currently returns them twice. 如果用户属于这两个类别,则当前将它们返回两次。 How can I return the user only once without using SELECT DISTINCT, regardless of how many categories they belong to? 无论用户属于多少类别,如何不使用SELECT DISTINCT仅返回一次用户?

You need a semi join. 您需要半联接。 This can be achieved with a sub query. 这可以通过子查询来实现。

SELECT * 
FROM user u 
WHERE EXISTS(SELECT * 
       FROM user_categories uc 
       WHERE u.user_id = uc.user_id AND  
       uc.category_id IN(1,3))

In MySQL the performance of sub queries is quite problematic however so a JOIN and duplicate elimination via DISTINCT or GROUP BY may perform better. 在MySQL中,子查询的性能存在很大问题,因此通过DISTINCTGROUP BY进行的JOIN和重复消除可能会更好。

I don't know about MySQL, but in Postgres you may get better performance in the semi-join version from 我不了解MySQL,但是在Postgres中,半连接版本可能会从

SELECT * FROM user u 
WHERE u.user_id 
IN (SELECT user_id FROM user_categories uc WHERE uc.category_id IN (1,3));

I would expect SELECT DISTINCT to run fastest but I have learned my expectations and DB performance are often much different! 我希望SELECT DISTINCT能够最快地运行,但是我已经了解到了我的期望,并且数据库性能通常大不相同!

Try using a GROUP BY 尝试使用GROUP BY

SELECT * FROM user u
JOIN user_categories uc ON u.user_id = uc.user_id
WHERE uc.category_id = 3 OR uc.category_id = 1
GROUP BY u.user_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM