How to make a request to group all the fields in one row?
Table:
id | type
---------
1 | 1
1 | 1
2 | 2
3 | 3
3 | 3
query:
select concat('id(', count(type), ')') from T group by id;
output:
id(2)
id(1)
id(2)
I want to get a string like: 'id(1)' = 2, 'id(2)' = 1, 'id(3)' = 2
If you do it in two stages. Make your
id(n) = xrecords, then GROUP_CONCAT() them.But do note that this is often a sign of a SQL Anti-Pattern.
It is normally recommended to not compress multiple values into a single value. And it is normally recommended to keep presentation and data layer separate.