【发布时间】:2015-11-01 17:42:42
【问题描述】:
我正在尝试编写一个 mysql 查询来以这种格式提取数据:
<table>
<caption>table title and/or explanatory text</caption>
<thead>
<tr>
<th>User ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Field Name 1</th>
<th>Field Name 2</th>
<th>Field Name 3</th>
<th>Field Name 4</th>
<th>Field Name 5</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query)) {
echo "<tr>
<td>" .$row{'user_id'}. "</td>
<td>" .$row{'first_name'}. "</td>
<td>" .$row{'last_name'}. "</td>
<td>" .$row{'field_name_1'}. "</td>
<td>" .$row{'field_name_2'}. "</td>
<td>" .$row{'field_name_3'}. "</td>
<td>" .$row{'field_name_4'}. "</td>
<td>" .$row{'field_name_5'}. "</td>
</tr>";
}
?>
</tbody>
</table>
数据库中的表格式如下。
表:用户数据
user_id | first_name | last_name |
------------------------------------------------------
1 | Jeff | Smith |
------------------------------------------------------
2 | Bob | Smith |
------------------------------------------------------
3 | Steve | Smith |
------------------------------------------------------
4 | Mary | Smith |
------------------------------------------------------
5 | Anna | Smith |
------------------------------------------------------
表格:custom_fields
custom_field_id | name |
-------------------------------------
3 | field name |
-------------------------------------
5 | field name |
-------------------------------------
7 | field name |
-------------------------------------
9 | field name |
-------------------------------------
11 | field name |
-------------------------------------
表格:custom_field_data
user_id | custom_field_id | value |
------------------------------------------------------
1 | 3 | XXXX |
------------------------------------------------------
1 | 5 | BBBB |
------------------------------------------------------
1 | 7 | CCCC |
------------------------------------------------------
1 | 9 | ZZZZ |
------------------------------------------------------
1 | 11 | YYYY |
------------------------------------------------------
2 | 3 | XXXX |
------------------------------------------------------
2 | 5 | BBBB |
------------------------------------------------------
2 | 7 | CCCC |
------------------------------------------------------
2 | 9 | ZZZZ |
------------------------------------------------------
3 | 3 | XXXX |
------------------------------------------------------
3 | 5 | BBBB |
------------------------------------------------------
3 | 9 | ZZZZ |
------------------------------------------------------
3 | 11 | YYYY |
------------------------------------------------------
我正在寻找查询数据然后使用 PHP 或 AJAX 将其打印到屏幕上的最佳解决方案。这可能吗?使用json会更好吗? 一个示例查询会很棒。
仅供参考:从长远来看,所有被提取的数据都需要在屏幕上进行过滤。感谢您的帮助。
我想要的输出是
user_id | first_name | last_name | custom_field_3 | custom_field_5 | custom_field_7 | custom_field_9 | custom_field_11 |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | Jeff | Smith | XXXX | BBBB | CCCC | ZZZZ | YYYY |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
2 | Bob | Smith | XXXX | BBBB | CCCC | ZZZZ | YYYY |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
3 | Steve | Smith | XXXX | BBBB | CCCC | ZZZZ | YYYY |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
4 | Mary | Smith | XXXX | BBBB | CCCC | ZZZZ | YYYY |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
5 | Anna | Smith | XXXX | BBBB | CCCC | ZZZZ | YYYY |
-------------------------------------------------------------------------------------------------------------------------------------------------------------
【问题讨论】:
-
user_id与custom_field有什么关系?value是来自custom_fields的name?这是加入文档,dev.mysql.com/doc/refman/5.0/en/join.html。应该是可行的,但不确定数据如何关联.. -
关系就像
user_data.user_id -> custom_field_data.user_id -> custom_fields.name...有意义吗? -
起初我误读了问题,以为第三张表是您想要的视图。我想这可能是你在
select * from user_data as ud join custom_field_data as cfd on cfd.user_id = ud.user_id join custom_fields as cf on cf.custom_field_id = cfd.custom_field_id之后的样子 -
@chris85 - 我设法编辑了您的查询,以获得我需要的数据部分。现在的问题是,它为每个自定义字段打印一行......我如何循环并生成上面的 HTML?
标签: php mysql ajax json entity-attribute-value