0

How do I get this code to show a price total for each customers order total amount for books? I tried using 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

?>
2
  • Not the most elegant solution, but is there a reason not to assign a variable $priceSum and do $priceSum += $row[price]; in your while loop? Commented Mar 2, 2011 at 20:48
  • Please don't post a wall of irrelevant code - you're asking about how to write a query, so we don't need to see any of the PHP or HTML code. Commented Mar 2, 2011 at 20:48

2 Answers 2

2
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;
Sign up to request clarification or add additional context in comments.

Comments

0

What was your SUM() query? 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";

2 Comments

Yeah, but that does not give me the desired output which should show total for each customer, not ALL customers orders combined.
the GROUP BY customers.name clause should do exactly what you ask: total for each customer. Marc B beat me to it - see his answer. The books.title and books.isbn are irrelevant in the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.