gpt4 book ai didi

mysql - 具有多列选择的 if 语句

转载 作者:行者123 更新时间:2023-11-29 00:17:27 25 4
gpt4 key购买 nike

我不知道如何解决这个问题,但我正在尝试根据 orders 表中的数据告诉我查看的位置从多个表中选择信息,第一个语句有效但是仅返回表 orders_bought_a 的信息,而不返回 orders_bought_b

的信息

如果订单来自位置a,那么我需要查询从表orders_bought_a中获取信息,对于位置b也应该这样做>.

有没有办法像我在 preferred 语句中那样在 mysql 中进行 block 选择?

如有任何帮助,我们将不胜感激,抱歉,如果这看起来一团糟,我已尽力而为。

当前声明:

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

最佳答案

我建议从一个表中选择数据并将其与其他表中的数据合并。
像这样:

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

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
WHERE oi.location = 'a'

UNION ALL

SELECT oi.id AS order_item_id, o.status, o.processed,
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_b obb ON obb.id = oi.buy_order_id
WHERE oi.location = 'b'
ORDER BY date_ordered DESC

关于mysql - 具有多列选择的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22490589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com