mysql查询A表type字段等于1的num字段合计值和type字段等于2的num字段合计值,
一:使用IF函数 IF(Condition,A,B)
当condition为true时,返回A,否则返回B...sql语句:
select sum(if(type=1,num,0)) as total_1,sum(if(type=2,num,0)) as total_2 from a ;
二:使用case when...ELSE [B]END
语句格式2:CASE condition WHEN value THEN [A] ELSE [B]END
当condition成立时,执行,否则执行B
sql语句:
select...=1 THEN num ELSE 0 END) as total_1,sum(CASE WHEN type=2 THEN num ELSE 0 END) as total_2 from a ;
或者
select