总结两个SELECT值

时间:2015-10-21 15:11:43

标签: php mysql

根据查询,我有一个问题。我想总结两个SELECT值(旧值+来自另一个表的新值)并更新旧值,如下所示:

a = (a++ < b++) ? a : b;

如何确定金额?

4 个答案:

答案 0 :(得分:2)

如果我理解你的问题,那么这样的事情就应该完美无缺。

$query = mysql_query("
    UPDATE Inventory iv
    SET quantity = quantity + (
      SELECT quantity 
      FROM Awaited aw
      WHERE aw.productname = iv.productname
    )
    WHERE iv.productname = '$AwaitedToMOVE';
");

答案 1 :(得分:1)

您应该只能SUM()在2个子查询中选择的2个字段,如下所示。

$query = mysql_query (
              "UPDATE Inventory 
               SET quantity = (
                               SELECT SUM(quantity) 
                               FROM Inventory 
                               WHERE productname='$AwaitedToMOVE'
                              ) +
                              (
                               SELECT SUM(quantity) 
                               FROM Awaited 
                               WHERE OrderName='$Awaited'
                              )
               WHERE productname='$AwaitedToMOVE'; ");

答案 2 :(得分:0)

我会将这些查询分成三个不同的查询,并使用PHP来计算总和。

$query_initial_value = mysql_query('SELECT quantity FROM Inventory WHERE productname = "' . $AwaitedToMove . '"');
$query_new_value = mysql_query('SELECT quantity FROM Awaited WHERE OrderName = "' . $Awaited . '"');
$sum = $query_initial_value + $query_new_value;
$query_update = mysql_query('UPDATE Inventory SET quantity = ' . $sum . ' WHERE productname = "' . $AwaitedToMove . '"');

答案 3 :(得分:0)

试试这个:

UPDATE Inventory SET Inventory.quantity = 
    ( Inventory.quantity +
         (SELECT Awaited.quantity FROM Awaited WHERE Awaited.OrderName = 'Condition') ) 
            WHERE Inventory.productname = 'Condition'