0

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!

RBR
  • 159
  • 1
  • 11

1 Answers1

0

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

raj
  • 819
  • 5
  • 9
  • Nice idea... That way i can run it in a single query... but in terms of performance, is it better to split every names field or have multiple queries? – RBR Jun 25 '13 at 12:40
  • Reducing database operations as much as possible would help improve an application's performance in general. So, I would go with the split method. – raj Jun 25 '13 at 21:10