我不知道 user_count 列。
这给出了由 3 或 18 使用的书籍
SELECT *
FROM books as Book
INNER JOIN books_users as BookUser
ON BookUser.book_id = Book.id
WHERE
Book.user_count = 2 AND
BookUser.user_id IN (3,18)
或者
SELECT *
FROM books as Book
INNER JOIN books_users as BookUser
ON BookUser.book_id = Book.id
WHERE
Book.user_count = 2 AND
(BookUser.user_id = 3 OR BookUser.user_id = 18)
编辑:这给出了 3 和 18 使用的书籍。
SELECT book.*
FROM books as Book
INNER JOIN books_users as BookUser1
ON BookUser1.book_id = Book.id
INNER JOIN books_users as BookUser2
ON BookUser2.book_id = Book.id
WHERE
Book.user_count = 2 AND
BookUser1.user_id = 3 AND
BookUser2.user_id = 18