查看数据透视表中的数据(Vertical mysql表)

时间:2015-01-07 13:33:26

标签: php mysql pivot-table

我真的对数据透视表感到困惑,请看看我有什么以及我需要实现的目标:

这是我的mysql表结构

id  meta_value            field_id   item_id

1   Ankur                 Fname      1
2   Gupta                 Lname      1
3   AAS Holdings          Company    1
4   Christie              Fname      2
5   Trojillo              Lname      2
6   AWS                   Company    2
7   Devadanam             Fname      3
8   Pulukuri              Lname      3
9   AAS Holdings          Company    3
10  Radha                 Fname      4
11  Neupane               Lname      4
......> There is no "Company" for that person
12  Marites               Fname      5
13  Torsar                Lname      5
14  AAS Holdings          Company    5
15  Muthanna              Fname      6
16  Shareef               Lname      6
17  GFTC                  Company    6

我需要能够以这种方式使用php mysql在HTML表格中显示数据

First Name       Last Name       Company

Ankur            Gupta           ASS Holdings
Christie         Trojillo        AWS

我还需要能够通过公司对该表进行排序。

这是我尝试做的,但我知道它完全错了,因为数据库只有Fname和Lname而没有公司会导致转义并使用下一个值并打破表格并且数据也可以存储以不同的顺序

$sql="SELECT * FROM system_frm_item_metas ORDER BY item_id";
$result=mysqli_query($con,$sql);
if ($result->num_rows > 0) {

echo "<table><tr><th>First Name</th><th>Last Name</th><th>Company</th></tr>";
while($row = $result->fetch_assoc()) {
   if ($row["field_id"] == "Fname") { echo "<tr><td>".$row["meta_value"]."</td>";}
   if ($row["field_id"] == "Lname") { echo "<td>".$row["meta_value"]."</td>";}
   if ($row["field_id"] == "Company") { echo "<td>".$row["meta_value"]."</td></tr>";}     
}
echo "</table>";

任何帮助都将非常感谢

1 个答案:

答案 0 :(得分:1)

DROP TABLE IF EXISTS eav_hell;

CREATE TABLE eav_hell
(entity INT NOT NULL
,attribute VARCHAR(12) NOT NULL
,value VARCHAR(30) NOT NULL
,PRIMARY KEY(entity,attribute)
);

INSERT INTO eav_hell VALUES
(1 ,'Fname','Ankur'),
(1 ,'Lname','Gupta'),
(1 ,'Company','AAS Holdings'),
(2 ,'Fname','Christie'),
(2 ,'Lname','Trojillo'),
(2 ,'Company','AWS'),
(3 ,'Fname','Devadanam'),
(3 ,'Lname','Pulukuri'),
(3 ,'Company','AAS Holdings'),
(4 ,'Fname','Radha'),
(4 ,'Lname','Neupane'),
(5 ,'Fname','Marites'),
(5 ,'Lname','Torsar'),
(5 ,'Company','AAS Holdings'),
(6 ,'Fname','Muthanna'),
(6 ,'Lname','Shareef'),
(6 ,'Company','GFTC');

SELECT entity
     , MAX(CASE WHEN attribute = 'Fname' THEN value END) Fname
     , MAX(CASE WHEN attribute = 'Lname' THEN value END) Lname
     , MAX(CASE WHEN attribute = 'Company' THEN value END) Company
  FROM eav_hell
 GROUP
    BY entity;

+--------+-----------+----------+--------------+
| entity | Fname     | Lname    | Company      |
+--------+-----------+----------+--------------+
|      1 | Ankur     | Gupta    | AAS Holdings |
|      2 | Christie  | Trojillo | AWS          |
|      3 | Devadanam | Pulukuri | AAS Holdings |
|      4 | Radha     | Neupane  | NULL         |
|      5 | Marites   | Torsar   | AAS Holdings |
|      6 | Muthanna  | Shareef  | GFTC         |
+--------+-----------+----------+--------------+

或(更快但不那么优雅)......

SELECT DISTINCT e.entity
              , f.value fname
              , l.value lname
              , c.value company
           FROM eav_hell e
           LEFT 
           JOIN eav_hell f
             ON f.entity = e.entity
            AND f.attribute = 'fname'
           LEFT 
           JOIN eav_hell l
             ON l.entity = e.entity
            AND l.attribute = 'lname'
           LEFT 
           JOIN eav_hell c
             ON c.entity = e.entity
            AND c.attribute = 'company';