我有两个 sql,它们给了我相同的结果:
查询一:
SELECT
u.*, COUNT(po.order_id) products_count
FROM (SELECT * FROM orders o WHERE o.date >= (CURRENT_DATE() - INTERVAL 1 MONTH)) o
LEFT JOIN products_orders po ON po.order_id=o.id
JOIN users u ON u.id=o.user_id
GROUP BY po.order_id
和查询 2:
SELECT
u.*, o.id order_id, COUNT(po.order_id) products_count
FROM users u
JOIN orders o ON o.user_id=u.id
LEFT JOIN products_orders po ON po.order_id=o.id
WHERE o.date >= (CURRENT_DATE() - INTERVAL 1 MONTH)
GROUP BY po.order_id