我不知道如何解決這個問題,但我試圖從多個表中選擇信息,這取決於表格中的數據來自於orders
表格,第一個語句可行,但僅限於如果一個訂單來自location a
然後我需要查詢抓住從表orders_bought_a
信息返回從orders_bought_b
如果列表選擇多列
從表orders_bought_a
,而不是信息,同樣應該爲location b
完成。
有沒有辦法在mysql中執行塊選擇,比如我在首選語句中完成它的方式?
任何幫助將不勝感激,對不起,如果這看起來像一團糟,這是我能做的最好的。
當前聲明:
SELECT oi.id AS order_item_id,
o.status,
o.processed,
oba.item_id AS buy_item_id,
oba.price AS buy_price,
oba.CONDITION AS buy_condition,
obb.item_id AS buy_item_id,
obb.price AS buy_price,
obb.CONDITION AS buy_condition
FROM order_items oi
LEFT JOIN books b
ON oi.isbn = b.isbn
LEFT JOIN orders o
ON o.id = oi.order_id
LEFT JOIN orders_bought_a oba
ON oba.id = oi.buy_order_id
LEFT JOIN orders_bought_b obb
ON obb.id = oi.buy_order_id
ORDER BY date_ordered DESC
首選(不工作):
SELECT oi.id AS order_item_id, o.status, o.processed,
CASE
WHEN(oi.location == 'a') THEN(
oba.item_id AS buy_item_id,
oba.price AS buy_price,
oba.condition AS buy_condition
)
WHEN(oi.location == 'b') THEN(
obb.item_id AS buy_item_id,
obb.price AS buy_price,
obb.condition AS buy_condition,
)
FROM orders_items oi
LEFT JOIN books b ON oi.isbn = b.isbn
LEFT JOIN orders o ON o.id = oi.order_id
LEFT JOIN orders_bought_a oba ON oba.id = oi.buy_order_id
LEFT JOIN orders_bought_b obb ON obb.id = oi.buy_order_id
ORDER BY date_ordered DESC
謝謝,我應該考慮以前使用UNION,我有時會忘記,但在進行比較時,您需要使用一個等號「oi.location ='b'' –
@ mk_89,更正爲使用單等號 –