笛卡尔积
实际问题
查询 A 表,关联 B 表。但是 B 表的记录可能存在多条。
原始
SELECT count(a.ID)
FROM A a
LEFT JOIN B b
ON a.order_no=b.order_no
WHERE ....
GROUP BY ...
Having...
修正
可以将 B 查询关联中用到的数据做一个子查询。
可以如下处理:
SELECT count(a.ID)
FROM A a
LEFT JOIN (SELECT DISTINCT order_no FROM B) b
ON a.order_no=b.order_no
WHERE ....
GROUP BY ...
Having...