输入banner图图片脚本导航/分类

MYSQL中有关SUM字段按条件统计使用IF函数(case)问题_MySQL

bitsCN.com

今天群里有人问了个问题是这样的:

然后有群友是这样回答的

select name,sum(case when stype=4 then money*(-1) else money end ) as M
from table
group by name

我想了想,应该可以用IF函数

于是改了下

select name,sum(money*IF(stype=4,-1,1)) as M
from table
group by name

两种方式那种更效率还未测试。

bitsCN.com