我有一个 SQL 删除查询
有 2 个表:report(其主键为 reportId 和一个名为 migerated 的位字段)和 report_detail(其外键为 reportId)。我想从report_detail中删除所有具有reportId的行,该reportId在报告表中已迁移= 1。这是选择我想要的所有行的选择查询:
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
)
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
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能会删除表中的所有内容。
试试这个:
That will likely delete everything in your table.
try this instead:
MySQL 有一种方法可以从特定表删除,而与其他表连接:
或者:
MySQL has a way to delete from a particular table, while joining with other tables:
Or: