我正在用PHP编辑程序,我想显示一个包含来自不同表的数据的表。 现在我有2个表,我只使用以下代码打印第一个表(registro_eu):
$sql = "SELECT * FROM registro_eu WHERE MONTH(data_conv) = 7 AND YEAR(data_conv) = 2016";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo '<div class="table-responsive">';
echo '<table class="table table-striped">';
echo '<thead>
<tr>
<th>Date</th>
<th>Desc</th>
</tr>
</thead>
<tbody>';
while($row = $result->fetch_assoc()) {
echo '
<tr>
<td>'. $row["data"].'</td>
<td>'. $row["desc"].'</td>
</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
}
TABLE REGISTRO_EU:
ID | DATE | DESC
-----------------------------
1 | 2016-06-15 | TEST DESCT
2 | 2016-06-15 | TEST 2
3 | 2016-06-15 | TEST 3
4 | 2016-06-16 | TEST DESCT
5 | 2016-06-16 | TEST 4
6 | 2016-06-17 | TEST 5
表FONDI:
ID | DATE | TOT
-----------------------------
1 | 2016-06-15 | 88
2 | 2016-06-16 | 100
3 | 2016-06-17 | 120
我希望向用户(使用php)显示如下内容:
DATE | DESC |TOT
-------------------------------
2016-06-15 | TEST DESCT |
2016-06-15 | TEST 2 |
2016-06-15 | TEST 3 |88
2016-06-16 | TEST DESCT |
2016-06-16 | TEST 4 |100
2016-06-17 | TEST 4 |120
答案 0 :(得分:1)
使用联接:
$sql = "SELECT * FROM registro_eu LEFT JOIN fondi ON registro_eu.DATE = fondi.DATE WHERE MONTH(data_conv) = 7 AND YEAR(data_conv) = 2016";
您必须消除WHERE子句中您正在谈论的表格的歧义,但我不了解您的架构,所以我会留给您。