简体   繁体   English

MySQL查询左联接条件问题

[英]Mysql Query Left Join Condition Problem

I have a litte problem with a mysql query. 我对mysql查询有疑问。

I use 5 tables: 我使用5张桌子:

user_has_data (uid, dataid); user_has_data(uid,dataid); users (uid, uname); 用户(uid,uname); user_in_group(uid, groupid, data); user_in_group(uid,groupid,data); groups(groupid, data, packageid); 组(groupid,数据,packageid); packages(packageid, name) 包(packageid,名称)

all ids are PK. 所有编号都是PK。 I want to build a sql query that finds a user, which belongs to a specified dataid, by its uname and checks if the user is in a group (relation in table user_in_group) belonging to a specified package (a group is assigned to one package). 我想构建一个sql查询,通过其uname查找属于指定dataid的用户,并检查该用户是否在属于指定包的组(表user_in_group中的关系)中(将一个组分配给一个包) )。 if so data from users, package and group should be fetched, otherwise only the user data should be fetched. 如果是这样,则应从用户,程序包和组中获取数据,否则应仅从用户数据中获取。 Therefore I use left joins, so I can also get the users with no group: 因此,我使用左联接,因此也可以使用户没有组:

SELECT `uac`.`uid`, `u`.`uid`, `uig`.`groupid`, `ag`.`packageid` 
FROM `user_has_data` AS `uac` 
INNER JOIN `users` AS `u` ON u.uid = uac.uid 
LEFT JOIN `user_in_group` AS `uig` ON uig.uid = uac.uid 
LEFT JOIN `groups` AS `ag` ON (ag.groupid = uig.groupid) AND (ag.packageid = 2) 
WHERE (uac.dataid = '3') AND (u.uname LIKE 'test%')
GROUP BY `u`.`uid`

Unfortunately I get wrong results: I get groups that have a different packageid than stated in the join, if the user has another group assigned to him with a different packageid. 不幸的是,我得到了错误的结果:如果用户分配给另一个具有不同packageid的组,则我得到的组的packageid与联接中所说明的不同。

probably this is because the first left join has no restrictions to packageid and the second is a left join and so it has no restrictions on the result (packageid is NULL for all results, but should have values). 可能是因为第一个左连接对packageid没有限制,第二个是左连接,所以它对结果没有限制(所有结果的packageid为NULL,但应该有值)。 If I change the second left join to a ordinary join, the group problem would be fixed but the query cant find users without group any more. 如果我将第二个左联接更改为普通联接,则组问题将得到解决,但查询无法再找到没有组的用户。

Any ideas how to fix this or even possible? 任何想法如何解决这个问题,甚至可能吗? thanks in advance! 提前致谢!

因为您将搜索限制为特定的组packageid'2',所以为什么不只创建两个LEFT JOIN INNER JOINS,然后在WHERE子句中放入ag.packageid = 2?

Are you saying that you are actually seeing the value ag.packageid = 2 in your query results? 您是说您实际上在查询结果中看到值ag.packageid = 2吗?

If not, I think you might try something like: 如果没有,我想您可以尝试以下方法:

SELECT `uac`.`uid`, `u`.`uid`, `g`.`groupid`, `g`.`packageid` 
FROM `user_has_data` AS `uac` 
INNER JOIN `users` AS `u` ON u.uid = uac.uid 
LEFT JOIN (`user_in_group` AS `uig` 
  INNER JOIN `groups` AS `ag` ON (ag.groupid = uig.groupid) AND (ag.packageid = 2) )
 AS `g` ON uac.uid = g.uid
WHERE (uac.dataid = '3') AND (u.uname LIKE 'test%')
GROUP BY `u`.`uid`
SELECT `uac`.`uid`, `u`.`uid`, `uig`.`groupid`, `ag`.`packageid` 
FROM `user_has_data` AS `uac` 
INNER JOIN `users` AS `u` ON u.uid = uac.uid 
LEFT OUTER JOIN `user_in_group` AS `uig` ON uig.uid = uac.uid 
LEFT OUTER JOIN `groups` AS `ag` ON ag.groupid = uig.groupid
WHERE (uac.dataid = '3') AND (u.uname LIKE 'test%')
AND (ag.packageid = 2 OR uig.uid IS NULL) 
GROUP BY `u`.`uid`

I know LEFT JOIN and LEFT OUTER JOIN mean the same thing, but I like to be explicit. 我知道LEFT JOIN和LEFT OUTER JOIN的意思是相同的,但我想明确一点。 With the condition in your join, I bet you were getting groups with different packages, but weren't getting the packages? 考虑到您加入的条件,我敢打赌,您正在使用不同的程序包来组,但是没有得到程序包吗?

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

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