我有两张桌子:
usStateCities:
和usstatescentroids:
SELECT state,COUNT(*) AS count FROM usStateCities GROUP BY state
给出每个州的城市数量
SELECT *
FROM (SELECT state, COUNT(*) AS count FROM usstatecities ) AS a,
usstatescentroids b
WHERE a.state = b.state
GROUP BY state
仅提供美国的一个州和一个城市总数 我想找到每个州的城市数量以及每个州的质心坐标(lon,lat)。
更正我已将状态更改为州。我剪切并粘贴了较旧的陈述。
答案 0 :(得分:4)
SELECT a.state, a.count, b.lon, b.lat
FROM (SELECT State, COUNT(*) AS count FROM usStateCities GROUP BY State) a
LEFT JOIN usstatescentroids b ON b.state = a.State
答案 1 :(得分:2)
我更喜欢使用join table然后使用子查询。试试吼叫:
SELECT b.State, b.lon, b.lat, COUNT(*) AS count
FROM usStateCities a, usstatescentroids b
WHERE a.state = b.state
GROUP BY b.State, b.lon, b.lat