我有 3 个表:books
、book_categories
、categories
。
book_categories
表“连接”books
和 categories
。它包含列:id
、book_id
、category_id
。
所以一本书可能属于多个类别,一个类别可能有很多书。
我需要查询从 given_category
中检索所有书籍,但属于 given_set_of_categories
的书籍除外。因此,例如,我想要所有来自 A 类的书籍,但前提是它们不属于 B 类或 C 类。我还需要按 Book.inserted 列对结果进行排序(排序)。
我知道如何使用 2 个连接从 given_category
中获取所有书籍,但不知道如何从结果中排除其他类别的一些书籍。我无法在 PHP 中过滤书籍,因为我正在对搜索结果进行分页。
最佳答案
where
category_id = <given category>
and books.book_id not in
(
select book_id from book_categories
where category_id in (<given set of cat>)
)
order by books.inserted
关于mysql - 选择一个类别中的所有书籍,但属于其他类别的书籍除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13408581/