Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-01 13:26:06
Original
1084 people have browsed it

bitsCN.com

今天群里有人问了个问题是这样的:
MYSQL中有关SUM字段按条件统计使用IF函数(case)问题_MySQL 
然后有群友是这样回答的

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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template