最近有个项目用到了 根据订单类型统计个数 还有根据订单类型计算金额
用到了 sum(if())和count(if()) 发现还是挺好用的
sum(if())
sum(if(a.order_sun_type=1,price,0))
sum函数返回一个值类型的数值,如果a.order_sun_type=1,则返回price
如果a.order_sun_type不等于1就返回0。
count(if())
count(IF(a.order_sun_type=0,TRUE,NULL))
count(if(a.order_sun_type=0,true,null)) 中count函数返回一个布尔值类型的数值,如果a.order_sun_type=0,返回true,如果order_sun_type不等于0返回null
如果写成count(If(order_sun_type=0,1,0) 则返回的全是true,也就是说全都会计数,而count()间断内容是true还是null,如果不是null就计数,如果是null就不计数。
计数还有一种方法
sum(if(a.order_sun_type=1,1,0))
如果type =1 则加1