I have three tables containing books and tags. You can tag multiple tags in a book.
What I want to do is filter out books in which both Comics and Romance are labeled.
- The first book is marked only by "Comics."
- The second book is marked as "Comics" AND "Romance", so this line should be returned in our example.
- The third book is marked only by Fantasy.
How to create the right query to find the second book?
.. select book_id from books b, tags t, tags_to_books tb where ((FIND TAGS_ID '7' AND '8')) ..
Table design:
DROP TABLE IF EXISTS `books`; CREATE TABLE `books` ( `book_id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`book_id`) ); INSERT INTO `books` (`book_id`) VALUES (1), (2), (3); DROP TABLE IF EXISTS `tags`; CREATE TABLE `tags` ( `tag_id` int(11) NOT NULL AUTO_INCREMENT, `tag_name` varchar(11) NOT NULL DEFAULT '', PRIMARY KEY (`tag_id`) ); INSERT INTO `tags` (`tag_id`, `tag_name`) VALUES (7,'Comics'), (8,'Romance'), (9,'Fantasy'); DROP TABLE IF EXISTS `tags_to_books`; CREATE TABLE `tags_to_books` ( `book_id` int(11) NOT NULL, `tag_id` int(11) NOT NULL ); INSERT INTO `tags_to_books` (`book_id`, `tag_id`) VALUES (1,7), (2,7), (2,8), (3,9);
Maria source share