Thread: SUM array

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Cairns Australia
    Posts
    17
    Rep Power
    0

    SUM array


    Hi Guys,

    below is a simple SQL query designed to extract a total from a single table.

    What I have been trying to do is take these two sums and add them together giving a result.

    Can anyone help me with the correct way to do this, no matter what I have tried I can't seem to get anywhere with it.

    I am not sure whether I exctract these two totals then add them together or whether there is a way to change my code to so that it adds up the columns from the two tables then displays a result with both figures added together. The only catch is that it is conditional (ie. SELECT SUM(total) AS total_price1 FROM detail_tourdet WHERE voucher_id = '$voucher_id'"
    FYI - voucher ID is a parameter passed from a previous page.


    Best Regards
    Steve


    <?php $sql = "SELECT SUM(total) AS total_price1 FROM detail_tourdet WHERE voucher_id = '$voucher_id'";
    $result = mysql_query ( $sql );
    $row=mysql_fetch_array($result);
    echo $row['total_price1']; ?>


    <?php $sql = "SELECT SUM(total) AS total_price2 FROM retail_acom WHERE voucher_id = '$voucher_id'";
    $result = mysql_query ( $sql );
    $row=mysql_fetch_array($result);
    echo $row['total_price2']; ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    Paris Uppland
    Posts
    2,378
    Rep Power
    391
    Code:
    select sum(total) AS total_price1
    from (select total  
    FROM detail_tourdet 
    WHERE voucher_id = '$voucher_id'
    union all 
    SELECT total 
    FROM retail_acom 
    WHERE voucher_id = '$voucher_id') dt

IMN logo majestic logo threadwatch logo seochat tools logo