In SQL, how to delete a row from one table if it does not have a corresponding row in another table?

How can i do:

REMOVE FROM foo WHERE id = 1 And bar does not contain id == 1

To find out how to remove a row id = 1from a table foo, only if barthere is no row in the table in the table id = 1.

+5
source share
3 answers
DELETE FROM foo WHERE id=1 AND NOT EXISTS (SELECT * FROM bar WHERE id=1)

I assume that you mean that foo and bar are tables, and you want to delete the entry from foo if it does not exist in the bar.

+19
source

using the connection:

delete f
from   foo f
left
join   bar b on
       f.id = b.id 
where  f.id = 1 and
       b.id is null
+11
source
0

All Articles