简体   繁体   English

使用INNER JOIN从两个SQL表中获取数据,显示HTML表

[英]Fetch data from two SQL tables with INNER JOIN, display HTML table

I am trying to display a table which will print out a list of themes I am creating for a forum software (there are several dozen), and display their version number from another table, by using an INNER JOIN statement. 我试图显示一个表,该表将打印出我为论坛软件创建的主题列表(有几十个),并使用INNER JOIN语句显示另一个表中的主题版本号。

Here's the HTML table I want to print: 这是我要打印的HTML表:

Theme Name     Version Number
-------------------------------
Elegance       1.7.0
Smarty         1.7.4
Aria           1.8.1
etc etc

The themes and their IDs are stored in xf_style table: 主题及其ID存储在xf_style表中:

--------------------------------
style_id   |  title
--------------------------------
1          |  Elegance
2          |  Smarty
3          |  Aria

The theme version numbers are stored in the options table xf_style_property . 主题版本号存储在选项表xf_style_property中 There's hundreds of options in the backend system, each with an option ID (style_property_id). 后端系统中有数百个选项,每个选项都有一个选项ID(style_property_id)。 The "Theme Version" option I'm looking for has ID of "5145". 我要查找的“主题版本”选项的ID为“ 5145”。

xf_style_property table 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

There are many repeating values in this table. 该表中有许多重复值。 Basically I want to fetch the property_value for each theme where the style_property_id equals 5145 , and inner join this with the xf_style table. 基本上,我想为style_property_id等于5145的每个主题获取property_value ,并将其与xf_style表进行内部连接。

My full script: 我的完整剧本:

<?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>

I've been trying a dozen different tweaks including this guide: https://www.w3schools.com/sql/sql_join.asp and other guides here at SE and can't seem to make it work. 我一直在尝试许多不同的调整,包括本指南: https : //www.w3schools.com/sql/sql_join.asp以及SE上的其他指南,但似乎无法使其正常工作。 Any help would be appreciated from a SQL newbie. SQL新手将提供任何帮助。

Disclaimer: the property_label column doesn't actually exist.. I only wrote it in for reader understanding. 免责声明:property_label列实际上不存在。.我仅将其编写为读者理解。 It's already known from another table which ID represents what option label. 从另一个表中已经知道哪个ID代表什么选项标签。

The where conditions are after the join where的条件是后join

This should fix it 这应该解决它

$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,;

Otherwise if you want to avoid repeated themes (even if they ahve diferent propety value) you can use Group By 否则,如果您要避免重复主题(即使它们具有不同的属性值),则可以使用“ 分组依据”

Your query should simply be 您的查询应该只是

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM