我有两张桌子:
table_subcategories - sub_categories_id, sub_category_name, sub_category_image_path
table_subcategory_categories - subcategory_id, category_id
我想从table_subcategories获取所有子类别,其中没有映射条目存在于" table_subcategory_categories"给定category_id
我正在使用此查询,但结果不正确。
my $sql_query = "SELECT TSC.sub_categories_id, TSC.sub_category_name, TSC.sub_category_image_path from table_subcategories as TSC
INNER JOIN
table_subcategory_categories as TSCC
ON
TSC.sub_categories_id = TSCC.subcategory_id
WHERE
TSCC.category_id != $cId ";
可以请一些人帮我解决这个问题吗?
答案 0 :(得分:1)
使用left join
并检查不匹配:
SELECT TSC.*
from table_subcategories TSC LEFT JOIN
table_subcategory_categories TSCC
ON TSC.sub_categories_id = TSCC.subcategory_id AND
TSCC.Category_Id = $cId
WHERE TSCC.category_id IS NULL;