删除孤立行的优雅方法? [英] Elegant way to remove orphan rows?

查看:112
本文介绍了删除孤立行的优雅方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中包含许多包含客户ID的历史记录条目.

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.

是否有一种简单的方法,无需遍历每个历史记录条目,就可以删除历史记录表中的所有行,其中由于删除了客户行,客户ID不再存在?

Is there an easy way, without looping through each history entry, to drop all rows in the history table where the customer ID no longer exists because the customer row was deleted?

推荐答案

delete from history_table where customer_id not in (select customer_id from customers)

您的意思是这样的吗?

这篇关于删除孤立行的优雅方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆