如何在MySQL中优化此查询?从该省取回市政府? TIA
select DISTINCT PM.municipality, count(*)
from anReference as R
inner join anFieldInfo as F on R.ref_id = F.ref_id
inner join tblProvince as PP on F.province_id = PP.province_id
inner join tblMunicipality as PM on F.municipality_id = PM.municipality_id
WHERE R.category = 0 AND R.test = 1
AND PP.region_id=2
AND PM.province_id=7
AND R.date_accessed between '2016-11-01 00:00:00' and '2016-11-30 23:59:00'
GROUP BY PM.municipality;
答案 0 :(得分:1)
如果你得到夸大的值,那么考虑重写类似的东西:
SELECT PM.municipality, count(*)
FROM PM
WHERE tblMunicipality AS PM.province_id=7
AND EXISTS ( ... )
GROUP BY PM.municipality
子查询(在EXISTS
中)是所有其他表的JOINs
。
暂且不说:这是测试一个月范围的“更简单”方式:
AND R.date_accessed >= '2016-11-01'
AND R.date_accessed < '2016-11-01' + INTERVAL 1 MONTH