SELECT中的COUNT()不产生所需的输出

时间:2012-10-13 16:59:30

标签: mysql sql

我有两张桌子:

usStateCities:
usStateCities

和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

仅提供美国的一个州和一个城市总数 我想找到每个州的城市数量以及每个州的质心坐标(lon,lat)。

更正我已将状态更改为州。我剪切并粘贴了较旧的陈述。

2 个答案:

答案 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