Marcelo Agimóvel
Marcelo Agimóvel

Reputation: 1719

Delete from table A where not in (two other tables)

I need to delete cities that are not being used in table properties and owners.

I found this code:

DELETE from Table_A 
WHERE  id -- ID of Table_A
       not in (select ID FROM Table_B)

How it would look with a C table?

Upvotes: 1

Views: 34

Answers (1)

Doncho Gunchev
Doncho Gunchev

Reputation: 2239

You can use union to join the results from table_B and table_C. Here is the documentation - http://www.mysqltutorial.org/sql-union-mysql.aspx. I have no mysql handy, but the following should work:

DELETE FROM Table_A WHERE id NOT IN (
    SELECT id FROM Table_B UNION DISTINCT SELECT id FROM Table_C
)

Upvotes: 3

Related Questions