user765368
user765368

Reputation: 20346

Reading unique data from mysql table

Let's say I have a table which can contains data like this:

id     name      class
--     ----      ------
 1    Joshua      math
 2     Mary      history
 3     Mary      chemistry
 4     Matt      physics 

As you can see, for "Mary", we have two classes "history" and "chemistry". How do I (with php or and SQL statement) get the data for "Mary" and return it like this:

Mary, history, chemistry

In other words, I want to get all the data in the table and return them like this:

Josuha, math
Marry, history, chemistry
Matt, physics

So whenever a user appears multiple times in the table, I get all his/her data in one line.

Upvotes: 1

Views: 63

Answers (1)

imm
imm

Reputation: 5919

Use MySQL's GROUP_CONCAT() aggregate function.

Upvotes: 3

Related Questions