Question

What i need to do is something like that:

Database:

name     |  category
---------|-----------
alan     |  foo
adam     |  foo
daniel   |  bar
mike     |  foobar
michael  |  foobar

To list like that:

FOO:
- Adam
- Alan

BAR:
- Daniel

FOOBAR:
- Michael
- Mike

So, i know i can make that list with a query litening the data with GROUP BY category (to to the headers), and than, making a query for each group, listening the names... But i was wondering is it possible to make that list with a single mysql query.

Is that possible?

Thanks!

Was it helpful?

Solution

select group_concat(name) as names, category from table_name group by category

You can split the names and list them after running the above query

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top