我有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