user999690
user999690

Reputation: 267

delete row if ID is same in another table

I have two tables (Table1 and Table2.) I want to delete all rows in table2 if the id exsits in table1

Code so far:

DELETE a.id, a.car, a.boat 
FROM Table2 a 
LEFT JOIN Table1 b ON b.id = a.id

tables:

Upvotes: 0

Views: 307

Answers (3)

CaffGeek
CaffGeek

Reputation: 22054

Test this in a rollback transaction first (good habit to always do this with deletes)

DELETE FROM table2
    WHERE table2.id IN (SELECT table1.id FROM table1)

Upvotes: 2

n8wrl
n8wrl

Reputation: 19765

DELETE Table2
  FROM Table2
  JOIN Table1 ON Table1.ID = Table2.ID

Upvotes: 5

Zohaib
Zohaib

Reputation: 7116

Delete from table2 where table2.ID IN (select id from table1)

Upvotes: 5

Related Questions