0

我想以表格格式显示这些数据,比如

<table border='1'>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>City</th>
        <th>State</th>
        <th>Phone</th>
    </tr>

但是我的表格,按列,就像

userdbelemnts_id     userdbelements_field_name      userdbelements_field_value  

180                  user_first_name                Demo
181                  user_last_name                 Agent
183                  City                           Mumbai
184                  zip                            400000
185                  state                          xyz
189                  phone                          123456

如何展平标准化数据以显示在上述表格结构中?

4

1 回答 1

1

您可以对查询中的输出数据进行非规范化,如下所示(Grouping on userdb_id):

SELECT
    MAX(CASE WHEN userdbelements_field_name = 'user_first_name' 
        THEN userdbelements_field_value ELSE NULL END) AS first_name,
    MAX(CASE WHEN userdbelements_field_name = 'user_last_name' 
        THEN userdbelements_field_value ELSE NULL END) AS last_name,
    MAX(CASE WHEN userdbelements_field_name = 'City' 
        THEN userdbelements_field_value ELSE NULL END) AS city,
    MAX(CASE WHEN userdbelements_field_name = 'state' 
        THEN userdbelements_field_value ELSE NULL END) AS state,
    MAX(CASE WHEN userdbelements_field_name = 'zip' 
        THEN userdbelements_field_value ELSE NULL END) AS zip,
    MAX(CASE WHEN userdbelements_field_name = 'phone' 
        THEN userdbelements_field_value ELSE NULL END) AS phone,
FROM userdbelemnts
GROUP BY userdb_id

然后在您的 php 中,只需像在平面表中一样循环遍历结果。

<thead>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>City</th>
        <th>State</th>
        <th>Phone</th>
    </tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
    <tr>
        <td><?=$row['first_name']?></td>
        <td><?=$row['last_name']?></td>
        <td><?=$row['city']?></td>
        <td><?=$row['state']?></td>
        <td><?=$row['phone']?></td>
    </tr>
<?php } ?>
</tbody>

编辑:根据以下评论给出您的新表架构:

SELECT
    orodha_en_userdb.*,
    MAX(CASE WHEN userdbelements_field_name = 'user_first_name' 
        THEN userdbelements_field_value ELSE NULL END) AS first_name,
    MAX(CASE WHEN userdbelements_field_name = 'user_last_name' 
        THEN userdbelements_field_value ELSE NULL END) AS last_name,
    MAX(CASE WHEN userdbelements_field_name = 'City' 
        THEN userdbelements_field_value ELSE NULL END) AS city,
    MAX(CASE WHEN userdbelements_field_name = 'state' 
        THEN userdbelements_field_value ELSE NULL END) AS state,
    MAX(CASE WHEN userdbelements_field_name = 'zip' 
        THEN userdbelements_field_value ELSE NULL END) AS zip,
    MAX(CASE WHEN userdbelements_field_name = 'phone' 
        THEN userdbelements_field_value ELSE NULL END) AS phone,
FROM orodha_en_userdbelements
    INNER JOIN orodha_en_userdb
    ON orodha_en_userdbelements.userdb_id = orodha_en_userdb.userdb_id
WHERE orodha_en_userdb.userdb_id = $id
GROUP BY orodha_en_userdb.userdb_id
于 2013-06-15T12:43:57.243 回答