2011-03-06 67 views
0

我正在處理一個庫存php/mysql應用程序。SQL連接和結果問題

我有一個主表(inv_main),它將持有實際持有此設備的equipmentID和userID。 然後我有一個設備表(inv_equip),它包含有關設備的信息,以及誰下單的順序,以及誰的訂單,這兩個表都鏈接回用戶ID。

我試圖返回equipmentID,OWNERNAME,orderedByName的結果,orderedForName

當我運行下面的查詢,我得到了orderedByName空值,並OrderedForName應該都返回名稱作爲這些領域有標識,並不爲空。

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回報,我記錄在我的用戶表中的每個用戶。不同的是我沒有指定返回一個特定的ID,但所有的數據庫。

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的名稱?

回答

0

你的第二和第三個加入指定對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 

**只是在那裏重點。

+0

感謝您的快速響應和很好的修復 – thucker 2011-03-06 17:22:12

1

由於每次有INV_USER表引用,每個JOIN都需要ITS對應的別名,因此加入ON條件中出現了錯誤的別名。你有他們都指向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 
+0

感謝您的快速響應和很好的修復 – thucker 2011-03-06 17:22:37