所以我是 MySQL 新手,遇到了一些麻烦。我有一张名为 book_genres 的桌子和另一张名为 books 的桌子。
book_genres
+-------+---------+
|book_id| genre |
+-------+---------+
| 1 | Horror |
| 1 | Comedy |
| 2 | Romance |
| 2 | Comedy |
+-------+---------+
books
+-------+---------+
|book_id| title |
+-------+---------+
| 1 | A Book |
| 2 | B Book |
| 3 | C Book |
+-------+---------+
我正在使用以下命令来提取所有具有 3 个选定流派的 book_id:
SELECT DISTINCT a.book_id, b.genre AS genre1, c.genre AS genre2, d.genre AS genre3
FROM book_genres a
JOIN book_genres b ON a.book_id = b.book_id AND b.genre LIKE 'Romance'
JOIN book_genres c ON a.book_id = c.book_id AND c.genre LIKE 'Action'
JOIN book_genres d ON a.book_id = d.book_id AND d.genre LIKE 'Comedy'
GROUP BY book_id
我现在要做的是使用在此搜索中找到的 book_ids 从 books 表中提取所有书名。我不确定是否有更简单的方法可以做到这一点,但这就是我能想到的全部。
感谢任何可以提供帮助的人!