Reuben Gomes Reuben Gomes - 4 months ago 22
MySQL Question

SQL Group by name

I have a table

city|locality
a | bc
a | dc
a | ef
a | gh
a | ij


I want to create a group by so that it shows

a |bc
|dc
|ef
|gh
|ij


I'm currently using

select(*) from 'tablename' group by city;

Answer

Not pretty sure if this is what you want, but if you want to get all rows from locality for a in one new cell, you can use this.

SELECT *, GROUP_CONCAT(locality) as localities FROM table GROUP BY city

This will output:

city    localities 
a       bc, dc, ef, gh, ij