sql利用在一条语句中count出不同的条件的值
过多繁琐的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
第二种,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
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
