penone
penone

Reputation: 792

PHP / Mysql / Html Table - Group by row

I have a mysql table that has 3 columns: sid, cid and data. The table looks like below:

|sid   ||cid     ||data            ||
|62    ||name    ||John            ||
|62    ||age     ||20              ||
|62    ||email   ||[email protected]   ||
|63    ||name    ||jane            ||
|63    ||age     ||30              ||
|63    ||email   ||[email protected]   ||

What I would like to do is, using php, have an html formatted as:

|sid   ||name    ||age || email          ||
|62    ||John    ||20  || [email protected]  ||
|63    ||jane    ||30  || [email protected]  ||

Is this possible?

Upvotes: 2

Views: 236

Answers (1)

UkFLSUI
UkFLSUI

Reputation: 5672

Yes, obviously it is possible. You can join the same table thrice with itself and get the output from that. May be this is what you want:

SELECT a.sid AS sid, b.data AS name, c.data AS age, d.data AS email
FROM 
test a inner join test b on b.sid = a.sid
inner join test c on c.sid = a.sid
inner join test d on d.sid = a.sid
WHERE b.cid = 'name'
AND c.cid = 'age'
AND d.cid = 'email'
GROUP BY a.sid;

And here's the sql fiddle: http://sqlfiddle.com/#!9/1714b/37

Upvotes: 1

Related Questions