Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alphabetically order in group_concat

i have a table CITY which have column Name

'Noida' 'Delhi' 'Lahor' 'Bagdad' 'New York' 'Bangkok' 'Londan' 'Dubai' 'Islamabad' 'Columbo'

through Group_concat i want that all city name come in alphabetically order in different rows as like given below

'Bagdad,Bangkok' 'Columbo' 'Delhi,Dubai' 'Islamabad' 'Lahor,Londan' 'New York,Noida'

like image 250
Ashutosh SIngh Avatar asked Oct 19 '25 03:10

Ashutosh SIngh


1 Answers

Your are looking for the order by clause in group_concat() along with aggregation by the first letter in the string:

select group_concat(name order by name)
from cities
group by left(name, 1)
order by left(name, 1);
like image 169
Gordon Linoff Avatar answered Oct 21 '25 17:10

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!