Reputation: 22403
There are 2 tables: report (which has a primary key of reportId and a bit field called migrated) and report_detail (which has a foreign key of reportId). I want to delete all the rows from report_detail that have a reportId which, in the report table, has migrated = 1. This is the select query that selects all the rows I want:
select *
from report r inner join report_detail d
on r.reportId = d.reportId
where migrated = 1
Will this delete query do what I want or am I doing something wrong?
delete from report_detail
where exists(
select *
from report r inner join report_detail d
on r.reportId = d.reportId
where migrated = 1
)
Upvotes: 2
Views: 136
Reputation: 4173
That will likely delete everything in your table.
try this instead:
delete d
from report_detail d
inner join report r
on r.reportId = d.reportId
where migrated = 1
Upvotes: 2
Reputation: 3131
DELETE FROM report_detail
WHERE
report_detail.reportId IN
(
SELECT reportId
FROM report
WHERE migrated = 1
)
Upvotes: 3
Reputation: 9617
delete from report_detail d
inner join report r
on r.reportId = d.reportId
where migrated = 1
Upvotes: 2