SQL从一个表中选择记录并将其用作另一表的输入以删除记录

发布于 2024-12-06 09:14:39 字数 415 浏览 0 评论 0原文

我有带有列的 table1 :

def_id, def_name, username etc

我有另一个表 table2 ,它有一些关联:

assoc_id,parent_id,child_id

parent_id , child_id 实际上是来自 Table1 的 def_id 。它们根据 GUI 中的父子关系用户操作插入到 Table2 中。

现在,我想从 Table1 中选择特定用户名的所有 def_id,然后使用该输入删除所有记录(如果这些 def_id 是 Table2 中的parent_id 或 child_id 的一部分)。

我如何在 SQL 中执行此操作?我使用的是Sybase数据库。

任何帮助将不胜感激

I have table1 with columns:

def_id, def_name, username etc

I have another table table2 which has some association:

assoc_id,parent_id,child_id

The parent_id , child_id are actually def_id's from Table1 . They get inserted into Table2 based on parent_child relation user action in GUI.

Now I want to select all def_id for a particular username from Table1 and then use that input to delete all the records if those def_ids are part of the parent_id or child_id from Table2.

How do I do this in SQL? I am using Sybase database.

Any help will be appreciated

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

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

发布评论

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

评论(3

别念他 2024-12-13 09:14:39
 Delete Table2
 Where parent_id In
     (Select def_id from table1
      Where username = @username) Or
     child_id In
     (Select def_id from table1
      Where username = @username)

或者

  Delete t2
  From table2 t2
  Where Exists
     (Select * From Table1
      Where def_id In 
          (t2.parent_Id, t2.child_Id))
 Delete Table2
 Where parent_id In
     (Select def_id from table1
      Where username = @username) Or
     child_id In
     (Select def_id from table1
      Where username = @username)

Or

  Delete t2
  From table2 t2
  Where Exists
     (Select * From Table1
      Where def_id In 
          (t2.parent_Id, t2.child_Id))
奢欲 2024-12-13 09:14:39

一种简单的方法是将子查询添加到 where 子句。

DELETE ab
FROM AuthorArticle AS ab
WHERE ab.AuthID=(SELECT AuthID FROM Authors WHERE AuthorLastName='Yin')

从未使用过 Sybase,但这是基本的 SQL,应该可以工作。

An easy way is adding a subquery to the where clause.

DELETE ab
FROM AuthorArticle AS ab
WHERE ab.AuthID=(SELECT AuthID FROM Authors WHERE AuthorLastName='Yin')

Never used Sybase, but this is basic SQL and should work.

属性 2024-12-13 09:14:39

尝试:

DELETE table2
FROM table2
INNER JOIN table1 ON table1.def_id IN (table2.parent_id, table2.child_id)

Try:

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