我需要在CSV文件中导出所有带有折扣代码和折扣产品的订单。
我已完成此查询:
SELECT * FROM `ps_orders` WHERE `date_add` BETWEEN '2017-01-01
00:00:00' and '2017-03-31 23:59:59' AND `total_discounts` > 0 AND
`current_state` LIKE 4
现在我有一个带折扣代码的订单清单,但我还需要有折扣产品的订单。
我该怎么办? 谢谢,祝你有愉快的一天。
答案 0 :(得分:0)
您可以将左连接与订单详细信息一起使用:
SELECT *
FROM `ps_orders` o
LEFT JOIN `ps_order_detail` od
ON o.`id_order` = od.`id_order`
WHERE o.`date_add` BETWEEN '2017-01-01 00:00:00' and '2017-03-31 23:59:59'
AND (o.`total_discounts` > 0 OR od.`reduction_percent` > 0 OR od.`reduction_amount` > 0)
AND o.`current_state` LIKE 4
如果需要,您可以选择所需的字段,而不是*。 如果您只需要订单中的数据,请使用:
SELECT o.*
FROM `ps_orders` o
LEFT JOIN `ps_order_detail` od
ON o.`id_order` = od.`id_order`
WHERE o.`date_add` BETWEEN '2017-01-01 00:00:00' and '2017-03-31 23:59:59'
AND (o.`total_discounts` > 0 OR od.`reduction_percent` > 0 OR od.`reduction_amount` > 0)
AND o.`current_state` LIKE 4
GROUP BY o.`id_order`