您可以对查询中的输出数据进行非规范化,如下所示(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