Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Only output some values once

Tags:

select

mysql

I think I have done this before, but it could also be a function of PHP. What I would like is to do a MySQL query (in a MySQL client, not PHP) and get for example

Foo    A
       B
       C
Bar    B
       D
       E

Instead of

Foo    A
Foo    B
Foo    C
Bar    B
Bar    D
Bar    E

This would of course only make sence if it was ordered by that first column. Not sure if it is possible, but like I said, I mean to remember to have done this once, but can't remember how or if it was through some PHP "magic"...


Update: Suddenly remembered where I had used it. What i was thinking of was the WITH ROLLUP modifier for GROUP BY. But I also discovered that it doesn't do what I was thinking here, so my question still stands. Although I don't think there is a solution now. But smart people have proved me wrong before :P


Update: Should probably also have mentioned that what I want this for is a many-to-many relationship. In the actual select Foo would be the first name of an attendee and I would also want last name and some other columns. The A, B, C, D, E are options the attendee has selected.

attendee (id, first_name, last_name, ...)
attendees_options (attendee_id, option_id)
option (id, name, description)
like image 835
Svish Avatar asked Nov 15 '22 06:11

Svish


1 Answers

This will give you

Foo    A,B,C
Bar    B,D,E    

SELECT column1, GROUP_CONCAT(column2) FROM table GROUP BY column1
like image 148
Jon Snyder Avatar answered Dec 15 '22 05:12

Jon Snyder