3

我有 2 张桌子:

美国各州城市:
美国各州城市

和 usstatescentroids:
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

只给出美国的一个州和城市总数我想找到每个州的城市数量和每个州质心的坐标(经度,纬度)。

更正我已将状态更改为状态。我剪切并粘贴了较旧的语句。

4

2 回答 2

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
于 2012-10-13T17:10:01.067 回答
2

我更喜欢使用连接表然后子查询。试试下面:

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
于 2012-10-13T17:07:15.420 回答