Mysql Case 使用

select 
  sum(case when t.status in (1,3,4,5) then t.total_amount else 0 end) invlid_total_amount,
  sum(case when t.status in (2,6) then t.total_amount else 0 end) vlid_total_amount,
  sum(case when t.status in (1,3,4,5) then 1 else 0 end) invlid_tickets,
  sum(case when t.status in (2,6) then 1 else 0 end) vlid_tickets
from te_ticket t;

结果如下:

invlid_total_amount       vlid_total_amount       invlid_tickets       vlid_tickets

321288                            26420                            717                  105

你可能感兴趣的:(mysql,case)