SELECT orders.* FROM orders JOIN order_rows
ON orders.id = order_rows.order_id
WHERE order_rows.quant <> order_rows.quant_fulfilled
GROUP BY orders.id
ORDER BY orders.id DESC
我需要它包含没有相应 order_row 条目的行(这将是一个还没有项目的订单)。似乎必须有一种方法可以通过添加 ON 或 WHERE 子句来做到这一点?
在给定时间只会有几个空订单,因此如果最佳答案会显着降低性能,我将使用单独的查询。但我希望将它们包含在这个查询中,以便它们与其余的一起按 order.id 排序。只是不想为了包含 1-3 个没有商品的订单而使查询时间加倍。
只需使用
LEFT JOIN
而不是 JOIN
。您将获得所有行的订单。
SELECT orders.*
FROM orders
LEFT JOIN order_rows
ON orders.id = order_rows.order_id
AND order_rows.quant <> order_rows.quant_fulfilled
GROUP BY orders.id
ORDER BY orders.id DESC