SELECT cinfo.state, SUM(cstd.total_total_persons) as total, SUM(cstd.total_total_females) as girl FROM cinfo
JOIN cstd WHERE cinfo.id = cstd.College_id
GROUP BY cinfo.state
SELECT
cinfo.state,
SUM(cstd.total_total_persons) as total,
SUM(cstd.total_total_females) as girl
FROM cinfo
JOIN cstd
ON cinfo.id = cstd.College_id
GROUP BY cinfo.state
SELECT cinfo.state, SUM(cstd.total_total_persons) as total,
SUM(cstd.total_total_females) as girl
FROM cinfo JOIN
cstd
ON cinfo.id = cstd.College_id
GROUP BY cinfo.state;
2条答案
按热度按时间b1zrtrql1#
首先,在join中使用on子句,而不是where。
关于性能,请检查两个表上的索引。另外,回顾一下explain为您的查询显示的内容。
ru9i0ody2#
对于此查询:
可以在上添加索引
cstd(College_id, total_total_persons, total_total_females)
. 要优化查询,您所能做的已经不多了。