2016-12-24 82 views
0

拉着我有一個MySQL查詢在那裏我嘗試基於它的ID的另一個表中的一個表中查找一個產品名稱如下:的MySQL LEFT JOIN沒有結果

mysqli_query("SELECT product, expirydate, SUM(quantity), status FROM 
    stockmovement a LEFT JOIN (SELECT productid, product AS productname FROM 
    products) b ON a.product = b.productid WHERE a.status = '0' GROUP BY 
    a.product, a.expirydate HAVING SUM(a.quantity) > 0 ORDER BY a.product, 
    a.expirydate ASC"); 

一切從工作分開左連接,當我嘗試輸出'productname'時返回空白。任何人都可以看到查詢出錯的地方嗎?在

感謝提前聖誕快樂:)

+1

要加入'a.product'到'b.productid' ...是那是正確的?在不知道數據的情況下,它看起來像是想要'b.product'。 – PhillipXT

+0

對不起,字段名稱混亂。 stockmovement表中的'product'是一個ID,我試圖加入到產品表中的'productid'中,並在產品表中拉出產品名稱'product'。 – mckeegan375

回答

2

應該不是你的查詢是這樣的下面,而

SELECT a.product, a.expirydate, 
SUM(a.quantity), a.status, b.product as productname 
FROM stockmovement a 
LEFT JOIN products b ON a.product = b.productid 
WHERE a.status = '0' 
GROUP BY a.product, a.expirydate 
HAVING SUM(a.quantity) > 0 
ORDER BY a.product, a.expirydate;