如果两个字段中的任何一个位于一组值中,您将如何编写此 SQL 来删除

发布于 2024-08-18 09:29:15 字数 461 浏览 3 评论 0原文

背景:


我有2个表:

组件:(id,名称)

依赖项:(id,hasaComponentId,isaComponentId)

在这种情况下 >hasaComponentIdisaComponentId 都是由组件连接的组件表中的外键。id

问题


我有一组用户选择的 Id。我想要一个 sql 查询,该查询将从依赖项表中删除记录,其中 id 列表中的任何 id 都位于 hasaComponentId 字段或 isaComponentId 字段中。

此操作的最佳 sql 是什么?

Background:


i have 2 tables:

Components: (id, name)

Dependencies: (id, hasaComponentId, isaComponentId)

in this case the hasaComponentId and isaComponentId are both foreign keys into the components table joined by components.id

Question


i have a set of Ids that the user selects. I want a sql query that will delete records from the dependencies tables where any of the ids in my list of ids is either in the hasaComponentId field or the isaComponentId field.

what is the best sql for this action?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

似梦非梦 2024-08-25 09:29:15
DELETE FROM Dependencies WHERE hasaComponentId In (YOUR OTHER QUERY);
DELETE FROM Dependencies WHERE isaComponentId  In (YOUR OTHER QUERY);

或者

DELETE FROM Dependencies WHERE hasaComponentId In (YOUR OTHER QUERY) OR 
                               isaComponentId  In (YOUR OTHER QUERY);
DELETE FROM Dependencies WHERE hasaComponentId In (YOUR OTHER QUERY);
DELETE FROM Dependencies WHERE isaComponentId  In (YOUR OTHER QUERY);

OR

DELETE FROM Dependencies WHERE hasaComponentId In (YOUR OTHER QUERY) OR 
                               isaComponentId  In (YOUR OTHER QUERY);
情徒 2024-08-25 09:29:15
DELETE Dependencies
FROM Dependencies d, Components c
WHERE
 (  
  (c.id=d.hasaComponentID and d.isaComponentID is null)
  OR
  (c.id=d.isaComponentID and d.hasaComponentID is null)
 )
     AND c.id IN (1,2,3,5)

未经测试,但看起来不错。确保您首先执行SELECT

DELETE Dependencies
FROM Dependencies d, Components c
WHERE
 (  
  (c.id=d.hasaComponentID and d.isaComponentID is null)
  OR
  (c.id=d.isaComponentID and d.hasaComponentID is null)
 )
     AND c.id IN (1,2,3,5)

Not tested, but looks right. Make sure you perform a SELECT first!

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