我试图获得每个商店的所有累积销售额,即使值为null (没有条件的顺序),但左连接只给出具有对应关系的行,这不适合我:
SELECT s.identifierExt as StoreID,
YEAR(o.creation) AS Year,
MONTHNAME(o.creation) AS Month,
MONTH(o.creation) AS IDMonth,
ROUND(SUM(o.price), 2) AS Sales
FROM store s
LEFT JOIN order o ON o.store = s.id
AND (o.creation < '2018-09-13 00:00:00')
AND (o.place NOT IN ('PENDING','CANCELLED'))
AND (o.creation > '2018-01-12 00:00:00')
GROUP BY Year, Month, StoreID
ORDER BY IDMonth, StoreID ASC提前谢谢。
发布于 2019-04-16 16:10:02
这可能是因为INNER和OUTER joins之间的差异。内连接要求有一个对应的行(符合连接条件),而外连接没有这样的要求:它将返回NULL列。
看起来你想要一个外连接:
SELECT s.identifierExt as StoreID,
YEAR(o.creation) AS Year,
MONTHNAME(o.creation) AS Month,
MONTH(o.creation) AS IDMonth,
ROUND(SUM(o.price), 2) AS Sales
FROM store s
LEFT OUTER JOIN order o ON o.store = s.id
AND (o.creation < '2018-09-13 00:00:00')
AND (o.place NOT IN ('PENDING','CANCELLED'))
AND (o.creation > '2018-01-12 00:00:00')
GROUP BY Year, Month, StoreID
ORDER BY IDMonth, StoreID ASC通过这种方式,您还可以在没有订单的情况下获得商店。
另请参阅this answer on the difference between inner and outer joins
https://stackoverflow.com/questions/55703237
复制相似问题