写SQL语句,删除A表记录在B表不存在的数据

2024-11-20 03:22:24
推荐回答(5个)
回答1:

1. 采用not exists 执行速度快
delete from T_AD where not exists (select 1 from T_AD_PLAY where T_AD.N_CODE =T_AD_PLAY.N_CODE )
2. 采用not in 执行速度慢
Delete From T_AD Where N_AD Not In(Select N_AD From T_AD_PLAY)

回答2:

T_AD表中的哪个字段的记录在T_AD_PLAY表中的哪个字段的记录中不存在?
假设是删除T_AD表中N_CODE在T_AD_PLAY表中的N_CODE不存在的记录吧
delete from T_AD where T_AD.N_CODE not in (select T_AD_PLAY.N_CODE from T_AD_PLAY)

回答3:

delete from T_AD
where T_AD.N_CODE
not in (select T_AD.N_CODE from T_AD
where T_AD.N_CODE =T_AD_PLAY.N_CODE )

回答4:

delete from T_AD
where not exists (select 1 from T_AD_PLAY where T_AD.N_CODE =T_AD_PLAY.N_CODE )

这才是快的

回答5:

delete from T_AD where T_AD.N_CODE NOT EXISTS (SELECT N_AD FROM T_AD_PLAY)