PHP / Mysql / Html表 - 逐行分组

时间:2018-04-15 01:48:03

标签: php mysqli

我有一个包含3列的mysql表:sid,cid和data。该表如下所示:

|sid   ||cid     ||data            ||
|62    ||name    ||John            ||
|62    ||age     ||20              ||
|62    ||email   ||john@test.com   ||
|63    ||name    ||jane            ||
|63    ||age     ||30              ||
|63    ||email   ||jane@test.com   ||

我想做的是,使用php,将html格式化为:

|sid   ||name    ||age || email          ||
|62    ||John    ||20  || john@test.com  ||
|63    ||jane    ||30  || jane@test.com  ||

这可能吗?

1 个答案:

答案 0 :(得分:1)

是的,显然有可能。您可以将自己连接到同一个表三次并从中获取输出。可能这就是你想要的:

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;

这里是sql小提琴:http://sqlfiddle.com/#!9/1714b/37