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`;
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.