我有3个表db:file
,category
和file_to_category
。Mysql select查询特殊条件
我有类别ID,只需要选择仅属于这个类别的文件。
file_to_category
表结构是:
id
file_id
category_id
是有可能只用1查询呢?
我有3个表db:file
,category
和file_to_category
。Mysql select查询特殊条件
我有类别ID,只需要选择仅属于这个类别的文件。
file_to_category
表结构是:
id
file_id
category_id
是有可能只用1查询呢?
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
一个文件可以属于几个类别。我需要选择只有没有其他类别和属于当前类别的文件谢谢 – user3343663 2014-10-10 08:50:44
我更新了答案 – 2014-10-10 09:16:40
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
一个文件可以属于几个类别。我需要选择只有没有其他类别,属于当前类别的文件谢谢 – user3343663 2014-10-10 08:52:22
已更新答案 – 2014-10-10 09:22:34
一个文件可以属于多个类别。 我需要选择只有没有其他类别,属于当前类别的文件 谢谢 – user3343663 2014-10-10 08:49:04