sql语句count怎么使用?

如题所述

第1个回答  2022-11-16

过多繁琐的sql影响代码质量,及维护成本,以下为两种小技巧处理方式,仅供参考。

第一种,用case ---when---方法

select id

,sum(case when type in (1,2) then [count] else 0 end) as sum1

,sum(case when type in (3) then [count] else 0 end) as sum2

,sum(case when type in (4,5) then [count] else 0 end) as sum3

from 表名

group by id

第二种,if 判断

SELECT   SUM( goods_amount ) AS money,   

count( * ) AS num,  

count(if(pay_status=1,true,null)) AS success,   

count(if(pay_status=2,true,null)) AS fall   

FROM `tab_order_info`     

WHERE user_id = 11 

相似回答
大家正在搜