-- 1. 删除 ThreadedComment 扩展表DELETEFROM public.generic_threadedcomment WHERE comment_ptr_id IN ( SELECT id FROM public.django_comments WHERE comment ILIKE '%http://%'OR comment ILIKE '%https://%'OR comment ILIKE '%viagra%'OR comment ILIKE '%casino%' ); -- 2. 删除评论标记表DELETEFROM public.django_comment_flags WHERE comment_id IN ( SELECT id FROM public.django_comments WHERE comment ILIKE '%http://%'OR comment ILIKE '%https://%'OR comment ILIKE '%viagra%'OR comment ILIKE '%casino%' ); -- 3. 删除主评论表DELETEFROM public.django_comments WHERE comment ILIKE '%http://%'OR comment ILIKE '%https://%'OR comment ILIKE '%viagra%'OR comment ILIKE '%casino%';
✅ 特点:
每条 DELETE 独立执行,避免了 CTE 作用域问题。
按顺序删除,保证外键安全。
方法 2️⃣:使用临时表(如果垃圾评论非常多)
-- 创建临时表存垃圾评论 IDCREATE TEMP TABLE temp_spam_comments ASSELECT id FROM public.django_comments WHERE comment ILIKE '%http://%'OR comment ILIKE '%https://%'OR comment ILIKE '%viagra%'OR comment ILIKE '%casino%'; -- 删除 ThreadedComment 扩展表DELETEFROM public.generic_threadedcomment WHERE comment_ptr_id IN (SELECT id FROM temp_spam_comments); -- 删除评论标记表DELETEFROM public.django_comment_flags WHERE comment_id IN (SELECT id FROM temp_spam_comments); -- 删除主评论表DELETEFROM public.django_comments WHERE id IN (SELECT id FROM temp_spam_comments); -- 删除临时表DROPTABLE temp_spam_comments;
Comments
Comments
There are currently no comments
New Comment