1

This is my request

> DELETE FROM A WHERE id in (    Select
> id From A    Left Join B on A.id=B.id 
> Where B.id is null )

When I execute this I have this error

You can't specify target table 'A' for update in FROM clause

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mercer
  • 9,736
  • 30
  • 105
  • 170
  • possible duplicate of [SQL Delete: can't specify target table for update in FROM clause](http://stackoverflow.com/questions/45494/sql-delete-cant-specify-target-table-for-update-in-from-clause) – Neil Knight Apr 12 '11 at 08:23

3 Answers3

5

Maybe you could do it like this instead?

DELETE FROM A WHERE id NOT IN (SELECT DISTINCT B.id FROM B);
Niklas
  • 13,005
  • 23
  • 79
  • 119
1

You want to delete all records from table A that don't have a matching id in table B?

How about this:

DELETE
FROM A
WHERE NOT EXISTS (select 1 from B where A.id = B.id);
Sam Choukri
  • 1,874
  • 11
  • 17
1
DELETE FROM A
WHERE NOT EXISTS (
    SELECT *
    FROM B
    WHERE content_id = B.content_id
)
Marco
  • 56,740
  • 14
  • 129
  • 152