SQL连接和结果有问题

时间:2011-03-06 17:05:23

标签: sql join left-join

我正在开发一个库存php / mysql应用程序。

我有一个主表(inv_main),它将保存设备ID和实际拥有此设备的用户ID。 然后我有一个装备表(inv_equip),其中包含有关设备的信息,以及订购产品的人员以及订购的用户,都链接回用户ID。

我正在尝试返回equipmentID,ownerName,orderedByName,orderedForName

的结果

当我运行以下查询时,我获得了orderedByName和OrderedForName的NULL,它们都应返回名称,因为这些字段具有ID且不为null。

SELECT inv_equip.tech_id, inv_user.user_lname as ownedByName, 
orderFor.user_lname as orderForName, orderFrom.user_lname as orderFromName
FROM inv_main
LEFT JOIN inv_equip ON inv_main.main_equip_id = inv_equip.equip_id 
LEFT JOIN inv_user ON inv_main.main_user_id = inv_user.user_id 
LEFT JOIN inv_user as orderFor ON inv_equip.equip_ordered_for = inv_user.user_id  
LEFT JOIN inv_user as orderFrom ON inv_equip.equip_ordered_from = inv_user.user_id   
WHERE (inv_main.main_equip_id = 26)

这就是我的输出

tech_id                ownedByName  orderForName    orderFromName
TCH20110305_1299355914  admin     NULL            NULL

现在,如果我运行此查询,我将再次获得正确的ownedByname,但orderForName为我的用户表中的每个用户返回i记录。不同之处在于我没有指定返回某个id,而是在db中。

SELECT inv_equip.tech_id, inv_user.user_lname AS ownedByName, 
orderFor.user_lname AS orderForName, orderFrom.user_lname AS orderFromName
FROM inv_main
LEFT JOIN inv_equip ON inv_main.main_equip_id = inv_equip.equip_id
LEFT JOIN inv_user ON inv_main.main_user_id = inv_user.user_id
LEFT JOIN inv_user AS orderFor ON inv_equip.equip_ordered_for = inv_user.user_id
LEFT JOIN inv_user AS orderFrom ON inv_equip.equip_ordered_from = inv_user.user_id

我得到以下结果

                           OwnedBy OrderedFor   OrderedBy   
TCH20110304_1299257155  hucker  admin       NULL
TCH20110304_1299257155  hucker  hucker      NULL
TCH20110304_1299257155  hucker  beatty          NULL
TCH20110304_1299257155  hucker  Frank2          NULL
TCH20110304_1299257245  beatty  admin           admin
TCH20110304_1299257245  beatty  admin           hucker
TCH20110304_1299257245  beatty  admin           beatty

我正在寻找导致

的SQL查询
itemID  OwnedBy OrderedFor   OrderedBy   
 x      Hucker  Beatty       Frank

关于如何纠正我的连接逻辑以仅返回一个记录的受尊重用户ID的名称的任何提示?

2 个答案:

答案 0 :(得分:1)

由于每次都有INV_USER表引用,因此在连接ON条件上有错误的别名...每个JOIN都需要ITS相应的别名。你让他们都指向INV_USER。别名。

SELECT
      inv_equip.tech_id, 
      inv_user.user_lname as ownedByName, 
      orderFor.user_lname as orderForName, 
      orderFrom.user_lname as orderFromName
   FROM
      inv_main
         LEFT JOIN inv_equip 
            ON inv_main.main_equip_id = inv_equip.equip_id 
         LEFT JOIN inv_user 
            ON inv_main.main_user_id = inv_user.user_id 
         LEFT JOIN inv_user as orderFor 
            ON inv_equip.equip_ordered_for =  orderFor.user_id  
         LEFT JOIN inv_user as orderFrom 
            ON inv_equip.equip_ordered_from = orderFrom.user_id   
   WHERE
      inv_main.main_equip_id = 26

答案 1 :(得分:0)

您的第2次和第3次加入为第1次加入指定on条件:

LEFT JOIN inv_user 
ON        inv_main.main_user_id = inv_user.user_id 
LEFT JOIN inv_user as orderFor 
ON        inv_equip.equip_ordered_for = inv_user.user_id  
LEFT JOIN inv_user as orderFrom 
ON        inv_equip.equip_ordered_from = inv_user.user_id 

尝试修改代码,以便每个联接使用它自己的表:

LEFT JOIN inv_user 
ON        inv_main.main_user_id = inv_user.user_id 
LEFT JOIN inv_user as orderFor 
ON        inv_equip.equip_ordered_for = **orderFor**.user_id  
LEFT JOIN inv_user as orderFrom 
ON        inv_equip.equip_ordered_from = **orderFrom**.user_id 

**仅供强调。