Multiple COUNT() for multiple conditions in one query (MySQL)

后端 未结 6 1354
迷失自我
迷失自我 2021-02-01 02:53

I have these queries :

SELECT COUNT(*) FROM t_table WHERE color = \'YELLOW\';
SELECT COUNT(*) FROM t_table WHERE color = \'BLUE\';
SELECT COUNT(*) FROM t_table W         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 03:30

    SELECT 'yellow' as color ,COUNT(*) FROM t_table WHERE color = 'YELLOW'
    union
    SELECT 'blue' , COUNT(*) FROM t_table WHERE color = 'BLUE'
    union
    SELECT 'red',COUNT(*) FROM t_table WHERE color = 'RED';
    

    or

    select color, count(*) from table where color in ('red', 'blue', 'yellow') group by 1
    

提交回复
热议问题