我需要下面的mysql查询:
目前我正在进行如下个人查询:
SELECT count(*) FROM `myTable` WHERE `NAME` LIKE '%TYPE1%';
SELECT count(*) FROM `myTable` WHERE `NAME` LIKE '%TYPE2%';
SELECT count(*) FROM `myTable` WHERE `NAME` LIKE '%TYPE3%';
但我需要输出如下:
TYPEOF|COUNT(*)
===============
TYPE1|50
TYPE2|70
TYPE3|90
TYPE4|80
答案 0 :(得分:2)
select
case when `NAME` like '%TYPE1%' then 'TYPE1'
when `NAME` like '%TYPE2% then 'TYPE2'
...end as TYPEOF,
count (*)
from
`myTable`
group by
case when `NAME` like '%TYPE1%' then 'TYPE1'
when `NAME` like '%TYPE2% then 'TYPE2'
...end
或者,我是否太复杂了?您想要NAME列中的所有值吗?如果是,那么:
select
`NAME`,
count (*)
from
`myTable`
group by `NAME`