如果没有集合运算符,我如何才能找到任何涵盖三个不同 SQL 系统中的两个的书籍。我正在使用三个主题 id 'SSRV'、'ORA'、'MySQL' 并且需要输出任意三个中的 2 个。例如,这本书可能有一个主题 SSRV 和一个 ORA 主题,但没有 MySQL。这些主题的任何两个组合都足以通过我们的过滤器。
我想显示符合此测试的书籍的 book_id 和标题。
SELECT book_id
, title
FROM a_bkinfo.books B
WHERE book_id IN (
SELECT book_id
FROM a_bkinfo.book_topics BT
WHERE topic_id = 'SSRV' )
AND book_id IN (
SELECT book_id
FROM a_bkinfo.book_topics BT
WHERE topic_id = 'ORA' )
AND book_id IN (
SELECT book_id
FROM a_bkinfo.book_topics BT
WHERE topic_id = 'MySQL' )
AND book_id IN (
SELECT COUNT(*)
FROM (
SELECT book_id
, count(topic_id) AS topics
FROM a_bkinfo.book_topics BT
GROUP BY book_id
HAVING count(topic_id) = 2
) t
;
更新后的版本:
SELECT book_id
, title
FROM a_bkinfo.books B
WHERE book_id IN (
SELECT book_id
FROM a_bkinfo.book_topics BT
WHERE topic_id ='SSRV'
OR topic_id ='ORA' )
AND book_id IN (
SELECT book_id
FROM a_bkinfo.book_topics BT
WHERE topic_id ='SSRV'
OR topic_id ='MySQL')
AND book_id IN (
SELECT book_id
FROM a_bkinfo.book_topics BT
WHERE topic_id ='ORA'
OR topic_id ='MySQL');