SQL:计入左连接问题
SQL: Count in a Left Join issue
这打印出我期望的结果,但我不知道如何计算其中有多少行
select distinct(post.city), zips.zip_code, zips.population, post.stabbr
from zips left join
post
on zips.zip_code=post.zip
where post.stabbr like 'MO' and zips.population > 5000 and
post.longitud > (select avg (zips.lon) from zips where state_prefix like 'mo' )
group by post.city;
我以为
select count(*)
from zips left join
post
on zips.zip_code=post.zip
where post.stabbr like 'MO' and zips.population > 5000 and
post.longitud > (select avg (zips.lon) from zips where state_prefix like 'mo' )
group by post.city;
会工作,但它打印了所有 41 行,我相信城市的 grouped by
。
对我有什么想法吗?
我想你想要 count(distinct)
而没有 group by
:
select count(distinct post.city)
from zips left join
post
on zips.zip_code=post.zip
where post.stabbr like 'MO' and zips.population > 5000 and
post.longitud > (select avg(zips.lon) from zips where state_prefix like 'mo' );
这打印出我期望的结果,但我不知道如何计算其中有多少行
select distinct(post.city), zips.zip_code, zips.population, post.stabbr
from zips left join
post
on zips.zip_code=post.zip
where post.stabbr like 'MO' and zips.population > 5000 and
post.longitud > (select avg (zips.lon) from zips where state_prefix like 'mo' )
group by post.city;
我以为
select count(*)
from zips left join
post
on zips.zip_code=post.zip
where post.stabbr like 'MO' and zips.population > 5000 and
post.longitud > (select avg (zips.lon) from zips where state_prefix like 'mo' )
group by post.city;
会工作,但它打印了所有 41 行,我相信城市的 grouped by
。
对我有什么想法吗?
我想你想要 count(distinct)
而没有 group by
:
select count(distinct post.city)
from zips left join
post
on zips.zip_code=post.zip
where post.stabbr like 'MO' and zips.population > 5000 and
post.longitud > (select avg(zips.lon) from zips where state_prefix like 'mo' );