2016-08-02 114 views
-1

我試圖使用2個不同的標識符從3個不同的表中獲取信息。但不是返回明確的結果,而是返回我的數據而不是結果NULL。最奇怪的是,所有結果中有50%是好的,其他50%都缺少一個字段。即使結果不爲空,SQL連接也會返回NULL

SELECT inventory.exterior, 
     inventory.steamid, 
     inventory.icon, 
     inventory.id, 
     inventory.name, 
     inventory.steam_item_id, 
     items_prices.price, 
     bots.id AS botID 
FROM inventory 
     LEFT JOIN items_prices 
       ON items_prices.id = inventory.steamanalyst_id 
     LEFT JOIN bots 
       ON bots.steamid = inventory.steamid 
WHERE inventory.steamid IN (123, 123, 123, 123, 
           123 ... , 123) 
ORDER BY items_prices.price DESC 
LIMIT 100 offset 0 

所以這返回我: 有代替NULL S的關係是數字樣1,3,7,9等

enter image description here

,這是數據庫表的結構。 inventoryenter image description here

一切都很好用的語法,也有拼寫錯誤,沒有對錶名,行等,這只是不想收集的所有數據,並返回空值,而不是數據

+4

你知道'LEFT JOIN'是如何工作的嗎?如果連接左側的給定記錄與連接右側的任何記錄不匹配,則右側表格的列仍將出現,但值將全部爲NULL。我不確定這裏有什麼問題。 –

+0

錯誤的聯接?你的意思是使用內連接而不是左連接? LEFT將包含來自Inventory的所有記錄,僅包含與items_Prices匹配的記錄,然後僅包含與之前和漫遊器相匹配的記錄。 INNER Join將確保記錄存在於所有表中。 – xQbert

+0

顯示一些示例數據和表結構 – Jens

回答

0

NULL是因爲的LEFT JOIN。使用INNER JOIN如果你不希望看到他們:

SELECT i.exterior, i.steamid, i.icon, i.id, i.name, i.steam_item_id, 
     ip.price, b.id AS botID 
FROM inventory i JOIN 
    items_prices ip 
    ON ip.id = i.steamanalyst_id JOIN 
    bots b 
    ON b.steamid = i.steamid 
WHERE i.steamid IN (123, 123, 123, 123, 
         123 ... , 123) 
ORDER BY ip.price DESC 
LIMIT 100 OFSET 0; 

如果你想要的所有記錄,即使在bots表中沒有匹配,那麼就使用一個LEFT JOINbots

+0

歡迎來到俱樂部! - 它不是我btw –

相關問題