将多个字段数据分组为sql中的一个字段

时间:2016-09-26 06:20:22

标签: mysql group-by

我的SQL查询

select status,count(id) from table group by status;

返回以下数据

Resolved- 4
Closed - 12
Verified - 3
New* - 23
Unconfirmed* - 4
Needmoreinfo* - 5.

我希望mysql返回的是

Resolved- 4
Closed - 12
Verified - 3
Found* - 32

即新的,未经证实的,Needmoreinfo应被视为'发现'

2 个答案:

答案 0 :(得分:1)

你的问题缺少帮助你的信息..但也许你想要这个

SELECT 
CASE WHEN (status NOT IN ('Resolved', 'Closed', 'Verified')) THEN 'Found*' ELSE status END as status, count(id) 
FROM table 
GROUP BY CASE WHEN (status NOT IN ('Resolved', 'Closed', 'Verified')) THEN 'Found*' ELSE status END

答案 1 :(得分:0)

首先,我们要改变你的表格,即添加一个额外的列       LTER TABLE yourtable ADD COLUMN Found VARCHAR(50);

更新youtedable SET Found = CONCAT(New,' - ',未确认,',',Needmoreinfo);

现在你可以编写选择查询