How can I make:
DELETE FROM foo WHERE id=1 AND bar not contains id==1
To elaborate, how can I remove a row with id = 1
, from table foo
, only if there is not a row in table bar
with id = 1
.
Deleting rows based on another table. Sometimes we need to delete rows based on another table. This table might exist in the same database or not. We can use the table lookup method or SQL join to delete these rows.
How to Select All Records from One Table That Do Not Exist in Another Table in SQL? We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.
The basic syntax of the DELETE query Typical syntax of the DELETE query in SQL Server looks as follows: DELETE FROM <table_name> WHERE <condition>; Parameters: <table_name>: a table name you want to delete rows from.
DELETE FROM foo WHERE id=1 AND NOT EXISTS (SELECT * FROM bar WHERE id=1)
I'm assuming you mean that foo and bar are tables, and you want to remove a record from foo if it doesn't exist in bar.
using a join:
delete f
from foo f
left
join bar b on
f.id = b.id
where f.id = 1 and
b.id is null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With