MYSQL:查询结合计数

时间:2015-07-03 02:54:38

标签: mysql sql

sql:

SELECT 
id, COUNT(id) AS Counter
FROM tableA  
GROUP BY id

结果:

enter image description here

目标是如下所示:

enter image description here

将(NULL ID)与ID = 09结合起来,使其为0 + 1 = 1,给id正确命名也很好,比如

ID             Counter
NULLWITH09        1
THE 01           467
THE 02            8

任何帮助都会很棒..谢谢

1 个答案:

答案 0 :(得分:1)

未经测试,但是这样:

SELECT 
'THE ' +id, COUNT(id) AS Counter
FROM tableA
where id is not null and id<>9  
GROUP BY id
UNION ALL
SELECT 
'null and 9', COUNT(*) AS Counter
FROM tableA
where id is null or id=9