Elegant way to remove orphan rows?

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

    DELETE h.* FROM history h
    LEFT JOIN customer c ON h.customer_id = c.id
    WHERE c.id IS NULL
    

    I'm typing this from the top of my head, but you get the idea hopefully.

    Delete syntax documentation

提交回复
热议问题