mysql 中sum (if())

来源:互联网 发布:标签打印数据库 编辑:程序博客网 时间:2024/05/16 07:50
 

先来一个简单的sum

select sum(qty) as total_qty from inventory_product group by product_id

这样就会统计出所有product的qty.

但是很不幸,我们的系统里面居然有qty为负值。而我只想统计那些正值的qty,加上if function就可以了。 SQL为:

select sum(if(qty > 0, qty, 0)) as total_qty   from inventory_product group by product_id

意思是如果qty > 0, 将qty的值累加到total_qty, 否则将0累加到total_qty.

再加强一点:

select
sum( if( qty > 0, qty, 0)) as total_qty   ,
sum( if( qty < 0, 1, 0 )) as negative_qty_count
from inventory_product
group by product_id

这样就可以统计有多少条负值的记录了。方便程序用来告诉人们这条记录数据异常

原创粉丝点击