mysql 不一樣條件count ,多條件count()

 create table abc(A int,B int) sql

Select A,count(B) as total from ABC group by A
Select A,count(B) as total1 from ABC where B > 30 group by A
Select A,count(B) as totlal2 from ABC where B > 20 group by A

如何合併這三個查詢?
獲得一個查詢結果:
A,total,total1,total2post

答:code

Select A,
count(B) as total,
sum(case when B > 30 then 1 else 0 end) as total1,
sum(case when B > 20 then 1 else 0 end) as total2 
from ABC group by A

 

本身的例子blog

 select  count(1)  from origin_crx_data  where create_time like '2019-02-23%' and source='12345'table

 select  count(1)  from origin_crx_data  where active_time like '2019-02-23%' and source='12345'class

合併爲一條sqlselect

SELECT
sum(case when create_time like '2019-02-23%' then 1 else 0 end) as inst_count,
sum(case when active_time like '2019-02-23%' then 1 else 0 end) as active_count
FROM  origin_crx_data   where source='12345'im

相關文章
相關標籤/搜索