2

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.