Thread: SUM array

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

    Join Date
    Mar 2003
    Cairns Australia
    Rep Power

    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

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

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

    Join Date
    Jan 2003
    Paris Uppland
    Rep Power
    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