PHP显示合并数据表

时间:2013-04-26 08:13:56

标签: php mysql

对不起标题,很难获得冠军。但请尝试阅读下面的陈述。

我想合并具有相同名称项但具有不同信息的表中的数据。 表格示例:

form_no | name     | status | date_added
1       | keyboard | OK     | 19-APR-2013 (today)
2       | keyboard | NG     | 18-APR-2013 (yesterday)

现在我想在我的页面中显示它,只有1个信息合并为:
表视图示例:

No | Item Name | Yesterday Status | Today Status
1  | keyboard  | NG               | OK

就是这样,我想在表视图中显示它只有1个信息合并。它具有相同的名称,但具有基于date_added的不同状态。

<table>
<td>Item Name</td><td>Yesterday Status</td><td>Today Status</td>
</table>

include ("includes/_db_.php");

$today = date("j-F-Y");
$yesterday = date("j-F-Y", time() - 60 * 60 * 24);

$query = "SELECT * from t_production_status";
$result = mysql_query($query);

while ($data = mysql_fetch_array($result))
{
    $item_name = $data['name'];
    $status = $data['status'];
    $date_added = $data['date_added'];

<tr>
<td><?php echo $item_name; ?></td>
<td><?php echo $status; ?></td> <- this what I want show for yesterday status
<td><?php echo $status; ?></td> <- this what I want show for today status
<td><?php echo $date_added; ?></td>

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试在状态表上进行自我加入..

SELECT status1, status2 FROM t_production_status status1 JOIN t_production_status status1
WHERE status1.name = status2.name
AND status1.date = (today)
AND status2.data = (yesterday)

(未经过测试,但它应该是这些内容)

http://dev.mysql.com/doc/refman/5.0/en/join.html