有两个表:report(具有reportId的主键和名为migrated的位字段)和report_detail(具有reportId的外键)。我想从report_detail中删除所有具有reportId的行,该报告在报告表中已迁移= 1.这是选择我想要的所有行的select查询:
select *
from report r inner join report_detail d
on r.reportId = d.reportId
where migrated = 1
这个删除查询会执行我想要的操作还是我做错了什么?
delete from report_detail
where exists(
select *
from report r inner join report_detail d
on r.reportId = d.reportId
where migrated = 1
)
答案 0 :(得分:3)
DELETE FROM report_detail
WHERE
report_detail.reportId IN
(
SELECT reportId
FROM report
WHERE migrated = 1
)
答案 1 :(得分:2)
这可能会删除表格中的所有内容。
试试这个:
delete d
from report_detail d
inner join report r
on r.reportId = d.reportId
where migrated = 1
答案 2 :(得分:2)
delete from report_detail d
inner join report r
on r.reportId = d.reportId
where migrated = 1
答案 3 :(得分:1)
在与其他表连接时,MySQL可以从特定表中delete:
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
或者:
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;