Php MySQL查找不同数量和价格的商品的总销售额
本质上,我正在尝试创建一个HTML表,该表显示了MySQL中一个表中关于书籍和书籍销售的各种项目。基本上我需要Php MySQL查找不同数量和价格的商品的总销售额,php,html,mysql,Php,Html,Mysql,本质上,我正在尝试创建一个HTML表,该表显示了MySQL中一个表中关于书籍和书籍销售的各种项目。基本上我需要 要查找每本书的总销售金额,这些书可以是同一本书,但价格不同,数量也不同。到目前为止,我已经成功地在PHP中创建了一个循环来显示这样的表,它如下所示: 不幸的是,总销售价格计算不正确,我试图找出原因,因为我怀疑这与我的查询有关 以下是相关表格: 如表中所示,同一本书可以出现不同的时间,并且数量和单价也不同。我的一些计算是正确的,但另一些是不正确的。例如,图书ID103不正确 这是我的
<!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>
文件
图书报道:
身份证件
标题
量
总售价
以下使用联接的sql查询将帮助您实现目标。查询检索每本书(按Book.Id
和Book.Title
分组),存储在Quantity
中的销售总额和每次销售时的价格,通过将特定书籍的金额乘以当时每本书的单价来确定
选择
图书编号,
书名,
金额(销售数量)作为数量,
总价(销售单价*销售数量)作为价格
从…起
特价
内连接
Book-on-Book.Id=Sale.BookId
分组
书号,书名
我使用了相同的列名,因此这应该与php代码中的相同,例如:
$sql=“选择Book.ID、Book.Title、SUM(Sale.Quantity)作为数量,SUM(Sale.UnitPrice*Sale.Quantity)作为Book.ID上销售内部连接Book的价格=Sale.BookId按Book.ID、Book.Title分组”;
你不需要总和(数量*价格)吗?嗯,你可能是对的,你知道我将如何实现吗?只需乘以销售中的两个选择总和(数量*价格)。。那么为什么子查询呢?may by可通过ID从销售组中选择ID、标题、销售总额(数量)、销售总额(数量*单价)
非常感谢您的建议!