SQL query to count in group by with specific condition

前端 未结 2 1167
轮回少年
轮回少年 2021-01-24 12:28

My table data:

id      fieldId  Name   Text
----    ------  -----   ----
1        101    name1   a1
2        102    name2   a2
3        101    name1   a1
4               


        
相关标签:
2条回答
  • 2021-01-24 12:43

    Here.

    SELECT
        `Name`,
        COUNT(*) AS 'fieldId',
        COUNT(IF(`Text` REGEXP '^a[1-3]',1,NULL)) AS 'a1a2a3'
    FROM (SELECT * FROM `items` GROUP BY `Name`,`Text`) t1
    GROUP BY `Name`;
    
    0 讨论(0)
  • 2021-01-24 12:51
    select Name, 
        count(*) as CountAll, 
        count(case when Text like 'a1-%' or Text like 'a2-%' or Text like 'a3-%' then 1 end) as CountA1A2A3
    from MyTable
    group by Name
    

    You could also use a RegEx if you wish.

    0 讨论(0)
提交回复
热议问题