减去行返回更新表sql

时间:2014-04-05 16:49:13

标签: sql

我对更复杂的SQL很新,并且无法正确更新此代码

$sql = "UPDATE bank_accounts
    SET bank_accounts.balance = bank_accounts.balance - upgrades.cost 
    FROM upgrades
    INNER JOIN upgrades AS u
    WHERE u.id = :id
    AND
    bank_accounts.uid = :uid"

;

1 个答案:

答案 0 :(得分:0)

一些事情......

  1. :id和:uid来自哪里?
  2. 如果您将升级别名为'u',那么您始终必须使用'u',因此您无法输入'upgrades.cost'。它应该是'u.cost'。
  3. 您还在FROM子句中列出了两次“升级”。
  4. 试试这个吗?

    $sql = "UPDATE bank_accounts
              SET bank_accounts.balance = bank_accounts.balance - u.cost 
            FROM upgrades u INNER JOIN bank_accounts
                 ON u.id = back_accounts.id";
    

    另一个想法...... 更新让我感到紧张,所以我总是喜欢在进行更新之前对数据进行SELECT。

    SELECT bank_accounts.balance as BEFORE, bank_accounts.balance - u.cost as AFTER
    FROM bank_accounts, upgrades u
         ON u.id = bank_accounts.id