PHP MySQL Join Total Amount

Posted on 2007-09-28
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.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

    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 is not uniquw accross the different oders.amount you can't select it in the same query

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Creating and Managing Databases with phpMyAdmin in cPanel.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now