Elegant way to remove orphan rows?

后端 未结 5 2090
小蘑菇
小蘑菇 2021-02-19 02:51

I have a table with a lot of history entries that contain customer IDs.

There is a separate customer table. Occasionally some of the customer entries are removed.

<
5条回答
  •  情书的邮戳
    2021-02-19 03:37

    DELETE FROM CUSTOMER_HISTORY CH
    WHERE NOT EXISTS (SELECT 1 FROM CUSTOMER C WHERE C.CUSTOMER_ID = CH.CUSTOMER_ID)
    

提交回复
热议问题