如果int等于

时间:2019-03-28 17:01:25

标签: mysql sql

MySQL

我有以下查询

"SELECT Status, COUNT( ticket_id) AS total FROM tickets GROUP BY Status"

返回哪个

Status ¦ Total  
0      ¦   3
2      ¦   1
3      ¦   6

是否可以将结果更改为pharse,例如

0 =打开,1 =保留,2 =等待最终用户响应,3 =已解决,4 =关闭。

因此查询的结果将是

Status                 ¦ Total  
Open                   ¦   3
Awaiting end user      ¦   1
Resolved               ¦   6

任何帮助都会很棒, 干杯!

1 个答案:

答案 0 :(得分:3)

您需要case表达式:

select (case (Status) 
               when 0 then 'Open' 
               when 1 then 'Hold'
               when 2 then 'Awaiting End User Response'
               when 3 then 'Resolved'
               when 4 then 'Closed'
        end) as total, count(ticket_id) AS total 
from tickets t
group by (case (Status) 
               when 0 then 'Open' 
               when 1 then 'Hold'
               when 2 then 'Awaiting End User Response'
               when 3 then 'Resolved'
               when 4 then 'Closed'
          end);