从Postgresql删除记录

时间:2019-03-18 18:38:16

标签: sql postgresql sql-delete

我需要根据其他两个表上的内部联接条件从一个表中删除记录,但是查询会持续一段时间。

DELETE FROM public.blacklist WHERE subject_id NOT IN(
SELECT DISTINCT a.id FROM public.subject a
INNER JOIN stg.blacklist_init b ON a.subject_id=b.customer_code);

任何想法如何实现这一目标?

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用NOT EXISTS代替NOT IN,我认为您不需要DISTINCT

DELETE FROM public.blacklist bl
 WHERE NOT EXISTS  (
                     SELECT 0 
                       FROM public.subject a
                      INNER JOIN stg.blacklist_init b 
                         ON a.subject_id=b.customer_code
                      WHERE a.id = bl.subject_id 
                     );