du8442 2018-05-18 21:33
浏览 237
已采纳

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

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.

Here's the HTML table I want to print:

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:

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

The theme version numbers are stored in the options table xf_style_property. There's hundreds of options in the backend system, each with an option ID (style_property_id). The "Theme Version" option I'm looking for has ID of "5145".

xf_style_property table

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

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. Any help would be appreciated from a SQL newbie.

Disclaimer: the property_label column doesn't actually exist.. I only wrote it in for reader understanding. It's already known from another table which ID represents what option label.

  • 写回答

2条回答 默认 最新

  • dsriya5471 2018-05-18 21:40
    关注

    The where conditions are after the 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程