我正在使用MySQL和LEFT JOIN與子選擇。在任何情況下,我現在都已經設置好了,我不得不在我正在查詢的值「ELO-250030」中進行硬編碼。我希望能夠運行此腳本,而不必在我的值中硬編碼或將其傳遞到查詢中,我只是希望sql查詢循環遍歷數據庫表中的所有記錄,並匹配我目前所在的位置「ELO-250030 」。例如,如果我有ELO-250030,ELO-250031,ELO-250032的SKU。它需要查詢每個這些值。如何設置我的查詢下面,這樣我可以刪除:MySQL使用LEFT JOIN查詢所有記錄不只是一個
AND fbas.sku = 'ELO-250030'
和
AND fbai.sku = 'ELO-250030'
但MySQL將仍然匹配這些列和查詢......
這裏是我的全部sql查詢。
SELECT fbai.id,
fbai.sku,
sumQS,
SUM(fbai.qtyFulfillable) AS sumQF
FROM FBAInventory fbai
LEFT JOIN
(
SELECT fbas.sku, SUM(fbas.quantityShipped) as sumQS
FROM FBAShipment fbas
WHERE fbas.shipmentDate BETWEEN '2014-08-15 22:19:50' AND '2014-09-15 01:10:57'
AND fbas.sku = 'ELO-250030'
) fbas ON fbai.sku = fbas.sku
WHERE fbai.fbaInventoryReport_id = 62010
AND fbai.sku = 'ELO-250030'
GROUP BY fbai.id
ORDER BY sumQF DESC;
我已經試過這樣做,但我只是得到錯誤:
SELECT fbai.id,
fbai.sku,
sumQS,
SUM(fbai.qtyFulfillable) AS sumQF
FROM FBAInventory fbai
LEFT JOIN
(
SELECT fbas.sku, SUM(fbas.quantityShipped) as sumQS
FROM FBAShipment fbas
WHERE fbas.shipmentDate BETWEEN '2014-08-15 22:19:50' AND '2014-09-15 01:10:57'
AND fbas.sku = fbai.sku
) fbas ON fbai.sku = fbas.sku
WHERE fbai.fbaInventoryReport_id = 62010
GROUP BY fbai.sku
ORDER BY sumQF DESC;
刪除有問題的兩條線,並通過'GROUP更改GROUP BY fbai.sku' – cmorrissey 2014-09-30 18:27:20
當我做你的建議,我的「sumQS」一欄是空的所有記錄 – LargeTuna 2014-09-30 18:32:04
你是如何打算通過值'ELO-250030'到查詢?這個值來自哪裏? – Rahul 2014-09-30 18:36:39