简体   繁体   中英

mysql - count total entries for two variables

select county, count(county) as total_entry from house
group by county;

I get

county   | total_entry
'county1'| '1'
'county2'| '11'
'county3'| '2'

Now I want to get below result :

county   | ad_type     | total_entries 
'county1'| SALE        | '1'      
'county2'| SALE        | '4'      
'county2'| RENT        | '5'      
'county2'| NEWHOUSING  | '2'      
'county3'| RENT        | '2'        

How do I achieve this ? (ad_type is a field in house table).

Try something like this: (I'm assuming that your sum numbers are off because you want to group by ad_type as well)

SELECT county, COUNT(county) AS counties, ad_type FROM house
GROUP BY county, ad_type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM