Remove rows NOT referenced by a foreign key

前端 未结 2 1275
悲哀的现实
悲哀的现实 2021-02-09 11:42

This is somewhat related to this question:

I have a table with a primary key, and I have several tables that reference that primary key (using foreign keys). I need to

2条回答
  •  無奈伤痛
    2021-02-09 12:24

    DELETE
    FROM    group g
    WHERE   NOT EXISTS
            (
            SELECT  NULL
            FROM    table1 t1
            WHERE   t1.groupid = g.groupid
            UNION ALL
            SELECT  NULL
            FROM    table1 t2
            WHERE   t2.groupid = g.groupid
            UNION ALL
            …
            )
    

提交回复
热议问题