English 中文(简体)
Mysql查询左联接条件问题
原标题:Mysql Query Left Join Condition Problem
  • 时间:2010-10-15 18:41:43
  •  标签:
  • mysql
  • join

我对mysql查询有一个小问题。

我使用5张桌子:

user_has_data(uid,dataid);用户(uid,uname);user_in_group(uid、groupid、data);组(组ID、数据、包ID);软件包(软件包ID,名称)

所有id都是PK。我想构建一个sql查询,通过其uname查找属于指定dataid的用户,并检查该用户是否在属于指定包的组(表user_in_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`

不幸的是,我得到了错误的结果:如果用户分配了另一个具有不同packageid的组,那么我得到的组的packageid与联接中所述的不同。

这可能是因为第一个左联接对packageid没有限制,第二个是左联接,因此对结果没有限制(packageid对于所有结果都为NULL,但应该有值)。如果我将第二个左联接更改为普通联接,则组问题将得到解决,但查询无法再找到没有组的用户。

Any ideas how to fix this or even possible? thanks in advance!

最佳回答

你是说你实际上在查询结果中看到了值ag.packageid=2吗?

如果没有,我想你可以试试这样的方法:

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`
问题回答

因为您将搜索限制为2的特定组packageid,为什么不同时进行LEFT JOIN INNER JOIN,然后在WHERE子句中放入ag.packageid=2呢?

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`

我知道LEFT JOIN和LEFT OUTER JOIN的意思是一样的,但我喜欢显式。根据你加入的条件,我敢打赌你得到了有不同包裹的小组,但没有得到包裹?





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签