My table data:
id fieldId Name Text ---- ------ ----- ---- 1 101 name1 a1 2 102 name2 a2 3 101 name1 a1 4
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`;