kobayashi
kobayashi

Reputation: 41

Subtract Two Values from Different Table

I'm trying to subtract values from payment table and order1 table. This is the error "Subquery returns more than 1 row"

How can I subtract two values from different table?

The operation should be like this:

total (from order1 table) = total(from order1 table) - amount (from payment table)

$amount=$_POST['amount']; //payment table
//code inserting values in payment table    

//code for updating order1 table 
mysql_query("UPDATE order1 
         SET total=(SELECT total from order_details 
                    WHERE order_id='$order_id')- $amount
         WHERE order_id= '$order_id'
        ")or die(mysql_error());

Upvotes: 1

Views: 128

Answers (1)

void
void

Reputation: 7880

try this query:

     UPDATE order1 as o
     join order_details as d on o.order_id=d.order_id and o.order_id='$order_id'
     set o.total=d.total - $amount

Upvotes: 1

Related Questions