从表中删除数据时出现SQL错误-删除的目标和源

时间:2019-03-01 01:27:56

标签: mysql sql database

我正在尝试执行此查询。删除所有小于account表中所有帐户平均值的帐户。但是,我收到此错误:

#1093 - Table 'account' is specified twice, both as a target for 'DELETE' and as a separate source for data

查询为:

DELETE 
FROM account WHERE account_number IN 
(
  SELECT account_number FROM account 
WHERE balance < (SELECT AVG (balance) FROM account)
);

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

使用JOIN

DELETE a
FROM account a JOIN
     (SELECT AVG(balance) as avg_balance
      FROM account
     ) ab
     ON a.balance < ab.avg_balance;