sql:case when then else

case when 语句查询的是单条数据,若要查满足条件下的某个值的和,例如我要查is_check=1时的price的和:

应该用SUM(CASE is_check WHEN 1 THEN price END) AS checkPrice

而不是CASE is_check WHEN 1 THEN SUM(price) END AS checkPrice

select sum(case is_check when 0 then price end) AS noCheckPrice,
sum(CASE is_check WHEN 1 THEN price END) AS checkPrice,
SUM(price) as totalPrice 
from  t_subsidy_record

猜你喜欢

转载自blog.csdn.net/qq_36961530/article/details/107386291