Elegant way to remove orphan rows?

后端 未结 5 2117
小蘑菇
小蘑菇 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:25

    How about:

    DELETE FROM history_table 
    WHERE customer_id NOT IN (SELECT customer_id FROM customer);
    

提交回复
热议问题