PHP MySQL Join Total Amount

Posted on 2007-09-28
Medium Priority
Last Modified: 2013-12-13

I have two tables: "orders" and "view"
I have a view.php file which displays the division, name, amount, and member fields.

It pulls everything correctly, except the "amount" field. Each division has multiple orders and it only displays the amount of the first order. How can I display the total of all the amounts of each order by the "division"?

orderid - (primary key)

division - (primary key)

This is the mysql query that I have:

"SELECT view.division, view.members, orders.division, orders.name, orders.amount
FROM view, orders
WHERE view.division = orders.division"

This is the field that is suppose to display the amount of the order.

$cellvalue = "";
if ((!isset($_GET["edit_fd1"])) && (!isset($_POST["edit_fd1"]))) {
    $itemvalue = $row[1];
} else {
    $itemvalue = qsrequest("edit_fd1");

    $cellvalue = "<input type=\"text\" class=\"field1\" name=\"edit_fd1\" value=\"" . qsreplace_html_quote(stripslashes($itemvalue)) . "\" size=\"30\"  maxlength=\"24\" >";
    if ($cellvalue == "") {
        $cellvalue = "&nbsp;";
    echo $cellvalue;
Question by:fcruz5
1 Comment
LVL 48

Accepted Solution

hernst42 earned 2000 total points
ID: 19983178
If you have multiple orders for one division you need to GROUP the values in your SQL for the order. So the SQl looks like:

SELECT view.division, view.members, sum(orders.amount)
FROM view, orders
WHERE view.division = orders.division GROUP BY view.division, view.members

If orders.name is not uniquw accross the different oders.amount you can't select it in the same query orders.name

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question