[英]How to remove categories with no products?
我有點卡在以下 SQL 上:
delete from oc_category_description where 'category_id' NOT in (SELECT category_id FROM oc_product_to_category);
delete from oc_category_path where 'category_id' NOT in(SELECT category_id from oc_product_to_category);
delete from oc_category_to_store where 'category_id' NOT in(SELECT category_id from oc_product_to_category);
delete from oc_category_to_layout where 'category_id' NOT in(SELECT category_id from oc_product_to_category);
delete from oc_category_filter where 'category_id' NOT in(SELECT category_id from oc_product_to_category);
delete from oc_coupon_category where 'category_id' NOT in(SELECT category_id from oc_product_to_category);
delete from oc_category where 'category_id' NOT in(SELECT category_id from oc_product_to_category);
它刪除所有數據而忽略where
部分。 我究竟做錯了什么?
您需要刪除列名周圍的單引號,否則它會變成文字字符串,並且表達式不會執行您想要的操作。 它實際上檢查文字字符串'category_id'
是否屬於子查詢的結果集:因為它可能不屬於,所以所有行都被刪除。
另外,我建議使用exists
,即null
,而not in
則不是(也就是說,如果子查詢中的任何值為null
,則所有行都將被刪除)。
delete from oc_category
where not exists (
select 1
from oc_product_to_category pc
where pc.category_id = oc_category.category_id
);
為了提高性能,請考慮oc_product_to_category(category_id)
上的索引(或至少是此列首先出現的復合索引)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.