cppit
cppit

Reputation: 4564

mysqli return data in multiple rows with same id

hello so here is what I have in a table called "members"

id - key - value
1    name  john
1    age   35
1    zip   12345
2    name  stacy
2    age   22
2    zip   11223
3    name  mark
3    age   44 
3    ssn   11111111
3    zip   54321

I am trying to return this data in this manner:

1 - john - 35 - 12345
2 - stacey - 22- 11223
etc... (its a longer list)

I need it in this format that way I can insert it into another table using a loop. any ideas on how I can do that ?

$resultat = mysqli_query($con,"SELECT * FROM members'");

this will get all members. I am not sure how to go about doing what i need any input would be greatly appreciated thank you !!

Upvotes: 0

Views: 116

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269483

This is an example of pivoting the data. Here is one method:

select m.id,
       max(case when key = 'name' then value end) as name,
       max(case when key = 'age' then value end) as age,
       max(case when key = 'zip' then value end) as zip
from members m
group by id;

Upvotes: 2

Related Questions