我有一个 SQL 删除查询

发布于 2024-09-15 19:26:26 字数 512 浏览 9 评论 0原文

有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心意如水 2024-09-22 19:26:26
DELETE FROM report_detail
WHERE 
    report_detail.reportId IN
    (
        SELECT reportId 
        FROM report 
        WHERE migrated = 1
    )
DELETE FROM report_detail
WHERE 
    report_detail.reportId IN
    (
        SELECT reportId 
        FROM report 
        WHERE migrated = 1
    )
奶气 2024-09-22 19:26:26
delete from report_detail d 
inner join report r 
on r.reportId = d.reportId 
where migrated = 1
delete from report_detail d 
inner join report r 
on r.reportId = d.reportId 
where migrated = 1
波浪屿的海角声 2024-09-22 19:26:26

这可能会删除表中的所有内容。

试试这个:

delete d 
from report_detail d 
inner join report r  
    on r.reportId = d.reportId  
where migrated = 1

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
俏︾媚 2024-09-22 19:26:26

MySQL 有一种方法可以从特定表删除,而与其他表连接:

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;

MySQL has a way to delete from a particular table, while joining with other tables:

DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;

Or:

DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文