简体   繁体   English

在php中使用sql的sum字段

[英]Sum field using sql in php

How do I get this code to show a price total for each customers order total amount for books? 如何获取此代码以显示每个客户订单总额的价格总额? I tried using sum( 我尝试使用sum(

I have the following code: 我有以下代码:

<style type="text/css">
table{font-size:1.11em;}
tr{background-color:#eee; border-top:1px solid #333;}
</style>
<?php
$con = mysql_connect("localhost","root","pass");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("bookorama", $con);

$sql="SELECT customers.name, books.title, books.isbn, books.price 
         FROM customers, orders, order_items, books
         WHERE customers.customerID = orders.customerID 
         AND orders.orderID = order_items.orderID 
         AND order_items.isbn = books.isbn;";

$result = mysql_query($sql);     // You actually have to execute the $sql with mysql_query();
if($result === FALSE) {
    die(mysql_error()); // TODO: better error handling
}
echo "<h1 style='color:#3366ff;'>Each customer's book orders</h1>";
echo "<table>";  //start the table

while($row = mysql_fetch_array($result, MYSQL_ASSOC))  //Loop through the results
{
    //echo each row of the table
    echo "<tr>";              
    echo "<th><strong>Customer Name:</strong><br></th>";               
    echo "<td>$row[name]</td>";       
    echo "<th><strong>Book Title</strong><br></th>";                
    echo "<td>$row[title]</td>";
    echo "<th><strong>ISBN</strong><br></th>";  
    echo "<td>$row[isbn]</td>";
    echo "<th><strong>Book Price</strong><br></th>";  
    echo "<td>$row[price]</td>";
    echo "</tr>";
} 

echo '</table>';   //close out the table

?>
SELECT customers.name, SUM(books.price)
FROM customers, orders, order_items, books
WHERE customers.customerID = orders.customerID 
AND orders.orderID = order_items.orderID 
AND order_items.isbn = books.isbn
GROUP BY customers.customerID;

What was your SUM() query? 你的SUM()查询是什么? Something like: 就像是:

    $sql="SELECT customers.name, books.title, books.isbn, SUM(books.price)           
FROM customers, orders, order_items, books          
WHERE customers.customerID = orders.customerID           
AND orders.orderID = order_items.orderID           
AND order_items.isbn = books.isbn GROUP BY customers.name";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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