我在db中有3个表:file
,category
和file_to_category
。
我有类别ID,只需选择仅属于此类别的文件。
file_to_category
表结构是:
id
file_id
category_id
是否可以只使用1个查询?
答案 0 :(得分:0)
select f.*
from file f
join file_to_category fc on fc.file_id = f.id
group by f.id
having count(distinct fc.category_id) = 1
and sum(fc.category_id = $your_category_id) > 0
答案 1 :(得分:0)
select file.file_id, FileName,category.category_id,Category from file join file_to_category
on File.file_id=file_to_category.file_id join
category on category.category_id=file_to_category.category_id group by file.file_id
having count(file.file_id)=1