我有以下 MySQL 查询。
USE demo;
SELECT
loc.name,
sum(case cr_sex when 'F' then 1 else 0 end) as 'F#',
sum(case cr_sex when 'M' then 1 else 0 end) as 'M#'
FROM case_report_main as cr
JOIN location as loc on cr.cr_name = loc.name
GROUP BY loc.name
这按我想要的方式工作,它计算每个性别的每次出现并按位置名称对其进行分组。但是当我尝试将它与另一张桌子加入时
USE demo;
SELECT
loc.name,
sum(case cr_sex when 'F' then 1 else 0 end) as 'F#',
sum(case cr_sex when 'M' then 1 else 0 end) as 'M#'
FROM case_report_main as cr
JOIN location as loc on cr.cr_name=loc.name
JOIN map_polygons as mp on mp.polygon_name=loc.name
GROUP BY loc.name
这些值完全搞砸了,返回数千而不是预期的数十和数百。
我想我理解这个问题,附加表增加了行数。我不确定如何解决这个问题,或者这是否真的是问题所在。