从不同的表中减去两个值

时间:2015-03-06 08:47:24

标签: sql

我正在尝试从payment表和order1表中减去值。 这是错误“子查询返回超过1行”

如何从不同的表中减去两个值?

操作应该是这样的:

总计(来自order1表)=总计(来自order1表) - 金额(来自付款表)

$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());

1 个答案:

答案 0 :(得分:1)

尝试此查询:

     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