我有桌子:
stations (staion , phone , address)
AND
members (username , password , fullname , station)
我想要站点列表,计算用户名。所以我写了这个查询。
SELECT
stations.station as st,
(
SELECT COUNT(username)
FROM members
WHERE members.station = stations.station
) as co
FROM stations
GROUP BY stations.station
ORDER BY stations.station
警告(错误):
'ERROR: subquery uses ungrouped column "stations.station" from outer query'
请帮我用PostgreSQL获取正确的数据。
答案 0 :(得分:1)
正确的解决方案是:
SELECT
stations.station as st,
(
SELECT COUNT(username)
FROM members
WHERE members.station = ANY( array_agg( stations.station ) )
) as co
FROM stations
GROUP BY stations.station
ORDER BY stations.station
答案 1 :(得分:0)
试试这个
select stations.station as st,count(username) as co
from member left join stations on member.station=stations.station
group by stations.station
order by stations.station;
答案 2 :(得分:0)
没有join
的简单回答是:
SELECT stations.station, COUNT(username)
FROM stations, members
WHERE members.station = stations.station
GROUP BY stations.station
ORDER BY stations.station