【问题标题】:Find orphaned rows in three tables查找三个表中的孤立行
【发布时间】:2011-08-25 23:33:36
【问题描述】:

我有与文章结构有标签关系的树表。

树表是

1. tagnames_with_id
2. tag_id_relationship_with_article_id
3. article_with_id

现在,有时候文章被删除了,文章的标签关系也被删除了,但是现在的问题是,有时当交易没有正常完成时,表有没有被使用的数据。

  1. 如何找到所有不在关系中的标签ID 表但在标签表中并删除它们?这些是其标签 关系已被删除。

  2. 如何找到关系表中的所有标签 ID 但不在标签表中并删除它们?这些是标签 已被删除,但他们的关系仍然存在。

  3. 如何找到关系中的所有文章 ID,但 不在文章表中?这些是与标签的关系 已删除但标签关系仍然存在的文章 存在。

谢谢

【问题讨论】:

    标签: database entity-relationship maintenance


    【解决方案1】:

    这是这个问题的一般答案:

    select * from child_table
    where parent_id not in (select id from parent_table)
    

    此语法适用于我所知道的所有数据库

    【讨论】:

      【解决方案2】:

      类似于 Bohemian 的回答,但使用存在:

      select * from child_table where not exists 
      (select null from parent_table where child_table.parent_id = parent_table.id)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-17
        • 1970-01-01
        • 2019-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多