为什么下面的两个查询会给出不同的结果?我认为像下面那样加入的情况不应该相互影响,但它们显然是。
查询:
SELECT
s.id,
SUM(CASE WHEN n.template_id = 1 THEN 1 ELSE 0 END) AS template_1
FROM schedulings AS s
LEFT JOIN notes AS calls_made
ON s.id = calls_made.schedule_id
AND calls_made.template_id IN (1, 2, 3, 5, 6, 9, 10, 11, 12, 14)
LEFT OUTER JOIN notes AS n
ON s.id = n.schedule_id
WHERE s.id = 48810;
结果:
id template_1
48810 70
但是,如果我通过注释掉(或删除)第一个注释连接来更改查询,我会得到预期的结果。
查询:
SELECT
s.id,
SUM(CASE WHEN n.template_id = 1 THEN 1 ELSE 0 END) AS template_1
FROM schedulings AS s
LEFT OUTER JOIN notes AS n
ON s.id = n.schedule_id
WHERE s.id = 48810;
结果:
id template_1
48810 7