【问题标题】:Combing two SQL tables to create a singular table in HTML组合两个 SQL 表以在 HTML 中创建单个表
【发布时间】:2021-06-29 21:16:55
【问题描述】:

感谢您的帮助! 我遇到了一个复杂的问题,我正在尝试弄清楚如何解决它。 本质上,我正在尝试生成一个 HTML 表,该表从我的关于作者的 SQL 数据库中派生其所有值。在此表中,我试图显示 ID、他们的名字、姓氏以及每个作者写过的书总数。

我在 sql 中与两个表进行交互,但不知道如何将它们组合成一个表(在 HTML 中)。

第一个表由 ID、名字和姓氏组成。 第二个表由 AuthorID(外键引用 ID)和 BookID 组成。

基本上每个 AuthorID 出现的总次数就是他们写了多少本书。所以我需要找到一种方法将其合并到我先前存在的 select 语句中。

我已经成功地创建了一个带有 ID、名字、姓氏的表,但似乎可以弄清楚如何引用第二个表并计算实例。我已经包含了我的 HTML 和一些数据库截图。

HTML:

<!DOCTYPE html>
<html lang="en">
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="base.css">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <nav class="navbar navbar-default navbar navbar-inverse">
        <div class="container">
        <h2> <a class="head" href="base.php" style="text-decoration: none;">Database Project</a> <a class="btn btn-danger btn-sm main" href="#" role="button">Sign Out</a></h2>
        </div>
    </nav>

    <div class="container">
        <div class="jumbotron">
            <h3>Author Report:</h3><br>
            <table>
                <tr>
                    <th>ID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                </tr>
                <?php
                $conn = mysqli_connect("localhost","root","","Zion");
                if ($conn-> connect_error){
                    die("Connection Failed:". $conn-> connect_error);
                }
                $sql = "SELECT ID,FirstName,LastName from Author";
                $result = $conn-> query($sql);
                if($result->num_rows>0){
                    while($row = $result-> fetch_assoc()){
                        echo "<tr><td>". $row["ID"] . "</td><td>". $row["FirstName"]."</td><td>". $row["LastName"]. "</td></tr>";
                    }
                    echo"</table>";
                }
                $conn-> close()
                ?>


    </div>

</body>

SQL 图像:

谢谢!

目前我有什么:

【问题讨论】:

    标签: html mysql sql web-development-server


    【解决方案1】:

    要查找作者的所有书籍,您只需将查询更改为

    SELECT ID,FirstName,LastName,(SELECT count(bookId) from books where authorId = Author.id ) as noofbooks from Author
    

    此处子查询查找每个 AuthorId 的图书数量。

    <div class="container">
        <div class="jumbotron">
            <h3>Author Report:</h3><br>
            <table>
                <tr>
                    <th>ID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>No of Books</th>
        
                </tr>
                <?php
                $conn = mysqli_connect("localhost","root","","Zion");
                if ($conn-> connect_error){
                    die("Connection Failed:". $conn-> connect_error);
                }
                $sql = "SELECT ID,FirstName,LastName,(SELECT count(bookId) from books where authorId = Author.id ) as noofbooks from Author";
                $result = $conn-> query($sql);
                if($result->num_rows>0){
                    while($row = $result-> fetch_assoc()){
                        echo "<tr><td>". $row["ID"] . "</td><td>". $row["FirstName"]."</td><td>". $row["LastName"]. "</td><td>". $row["noofbooks"]. "</td></tr>";
                    }
                    echo"</table>";
                }
                $conn-> close()
                ?>
    
    
    </div>
    

    【讨论】:

    • 谢谢!当我引用 ID、FirstName 和 last name 时,我将如何引用此 select 语句以在 while 循环中使用?
    • 嘿,我实施了您的解决方案!再次非常感谢,但是我收到了这个奇怪的错误“注意:尝试在第 38 行的 /Applications/XAMPP/xamppfiles/htdocs/authorreport.php 中获取非对象的属性 'num_rows'”。任何想法为什么会这样?
    • @NoahSmiley 当您的查询字符串中有逻辑错误时会发生这种情况,即if($result-&gt;num_rows&gt;0) 返回一个空对象。确保您执行的查询字符串是正确的。因此,您应该将 if 更改为以下内容:` if($result && $result->num_rows)`。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多