[英]Fetch data from two SQL tables with INNER JOIN, display HTML table
我試圖顯示一個表,該表將打印出我為論壇軟件創建的主題列表(有幾十個),並使用INNER JOIN語句顯示另一個表中的主題版本號。
這是我要打印的HTML表:
Theme Name Version Number ------------------------------- Elegance 1.7.0 Smarty 1.7.4 Aria 1.8.1 etc etc
主題及其ID存儲在xf_style表中:
-------------------------------- style_id | title -------------------------------- 1 | Elegance 2 | Smarty 3 | Aria
主題版本號存儲在選項表xf_style_property中 。 后端系統中有數百個選項,每個選項都有一個選項ID(style_property_id)。 我要查找的“主題版本”選項的ID為“ 5145”。
xf_style_property表
--------------------------------------------------------------------- style_id | style_property_id | property_label | property_value --------------------------------------------------------------------- 1 | 5144 | Logo Size | 110px 2 | 5144 | Logo Size | 145px 3 | 5144 | Logo Size | 120px 1 | 5145 | Theme Version | 1.7.0 2 | 5145 | Theme Version | 1.7.4 3 | 5145 | Theme Version | 1.8.1
該表中有許多重復值。 基本上,我想為style_property_id等於5145的每個主題獲取property_value ,並將其與xf_style表進行內部連接。
我的完整劇本:
<?php
$servername = "localhost";
$username = "***";
$password = "***";
$dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT xf_style.title, xf_style_property.property_value FROM xf_style_property WHERE property_definition_id = 5145, INNER JOIN xf_style ON xf_style_property.style_id=xf_style.style_id";
$result = $conn->query($sql) or die($conn->error);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>Theme Name</th>
<th>Theme Version</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['title'] . "</td><td>" . $row['property_value'] . "</td></tr>";
}
?>
</tbody>
</table>
我一直在嘗試許多不同的調整,包括本指南: https : //www.w3schools.com/sql/sql_join.asp以及SE上的其他指南,但似乎無法使其正常工作。 SQL新手將提供任何幫助。
免責聲明:property_label列實際上不存在。.我僅將其編寫為讀者理解。 從另一個表中已經知道哪個ID代表什么選項標簽。
在where
的條件是后join
這應該解決它
$sql = "SELECT xf_style.title, xf_style_property.property_value FROM xf_style_property INNER JOIN xf_style ON xf_style_property.style_id=xf_style.style_id" WHERE property_definition_id = 5145,;
否則,如果您要避免重復主題(即使它們具有不同的屬性值),則可以使用“ 分組依據”
您的查詢應該只是
SELECT xfs.title, xfsp.property_value
FROM xf_style_property xfsp
INNER JOIN xf_style xfs ON xfsp.style_id = xfs.style_id
WHERE xfsp.style_property_id = 5145
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.