【问题标题】:Retrieving the sum of two independent columns - using nested select statements检索两个独立列的总和 - 使用嵌套选择语句
【发布时间】:2021-06-30 11:01:25
【问题描述】:

基本上,我试图显示书店内书籍的 ID、标题、总数量和总销售价格。目前我能够显示 ID、标题和总数量 - 或者 ID、标题和总销售价格,但是我无法弄清楚如何从该表中同时检索总数量和销售价格.

此查询成功地为我提供了 ID、标题和数量:

SELECT ID,Title,(SELECT Sum(Quantity) from Sale where BookId = Book.id ) as Quantity from Book;

此查询成功地为我提供了 ID、标题和销售价格:

SELECT ID,Title,(SELECT Sum(UnitPrice) from Sale where BookId = Book.id ) as Price from Book;

那么,如何在查询中同时检索 Quantity 和 Sales Price?这两个值都在同一个表中,但这里使用了两个表。

这是我当前用来循环并显示查询结果的 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="new.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="login.php" role="button">Sign Out</a></h2>
        </div>
    </nav>

    <div class="container">
    <div class="jumbotron">
        <h1>Book Report:</h1><br>
        <table>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Quantity</th>
                <th>Total Sale Price</th>
    
            </tr>
            <?php
            $conn = mysqli_connect("localhost","root","","Zion");
            if ($conn-> connect_error){
                die("Connection Failed:". $conn-> connect_error);
            }
            $sql = "SELECT ID,Title,(SELECT Sum(Quantity) from Sale where BookId = Book.id ) as Quantity,(SELECT Sum(UnitPrice) from Sale where BookId = Book.id ) as Price from Book";
            $result = $conn-> query($sql);
            if ($result = $conn->query($sql)) {
                while($row = $result->fetch_assoc()) {
                    echo "<tr><td>". $row["ID"] . "</td><td>". $row["Title"]."</td><td>". $row["Quantity"]. "</td><td>". "$".$row["Price"]. "</td></tr>";
                }
                echo"</table>";
            }
            $conn-> close()
            ?>
            <!-- Generate a report that for each book, displays the book id, the book title, 
            the quantity and the total sales price, sorts by the book id in increasing order; -->

</div>

</body>
</html>

谢谢!

【问题讨论】:

  • 您知道,您正在执行两次查询。 $result = $conn-&gt; query($sql); if ($result = $conn-&gt;query($sql)) { iterate the result set 还有一种更简单的方法。你不应该混合面向对象和过程mysqli_ 语法。使用面向对象的语法并始终如一地使用它。

标签: php html mysql sql


【解决方案1】:

美好的一天, 你可以试试这个查询

SELECT ID,Title,(SELECT Sum(Quantity) from Sale where BookId = Book.id ) as Quantity,(SELECT Sum(UnitPrice) from Sale where BookId = Book.id ) as Price from Book;

【讨论】:

  • 这个答案缺少教育解释。
【解决方案2】:

不要使用两个相关的子查询!使用聚合。一种方法是:

select b.id, b.title, Sum(s.Quantity) as quantity, sum(s.UnitPrice) as price
from Book b left join
     Sales s
     on s.bookid = b.bookid
group by b.id, b.title;

注意:不同数量的不同销售额的UnitPrice 总和对我来说似乎没有用。更有可能的是,您想要总价:

select b.id, b.title, Sum(s.Quantity) as quantity, 
       sum(s.UnitPrice * s.Quantity) as price
from Book b left join
     Sales s
     on s.bookid = b.bookid
group by b.id, b.title;

【讨论】:

  • 嘿,戈登,谢谢你帮助我!如何在我当前的 HTML 循环中实现它?我试过了,但我认为我做错了。谢谢!
  • @Night 您应该非常努力地实施 Gordon 的答案,因为它使用了更好的实践。在 SELECT 子句中使用子查询是个坏主意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 2021-04-07
相关资源
最近更新 更多