There are tables: book (id, title) tag (id, tag) bind_book_tag (id_book, id_tag)

The first is for books, the second is for tags, the third is for bundles of tags and books (by id, that is, in one row in the id_book column, the book id is specified from the book table and the id_tag ​​column is the tag id from the tag table). Each book has several tags. The id of each book is associated with the id of the desired tag in a separate line in the bind_book_tag table.

How for one book to pull out relevant (similar) books by tags? You can multiple requests. Application on php.

    1 answer 1

    Like that:

    select book.id, book.title from book join bind_book_tag bbt on book.id = bbt.id_book and bbt.id_tag in (select id_tag from bind_book_tag where id_book=$current_book_id) having count(bbt.id_tag) = (select count(id_tag) from bind_book_tag where id_book=$current_book_id) 

    It is possible to break this request into two. Those. first get the book tags, count them and replace the subqueries with the list of tags and the number of tags, respectively.

    As a development idea, you can check not the exact number of matching tags. For example, you can limit the bottom, so that at least one tag having count(bbt.id_tag)>0 : having count(bbt.id_tag)>0 . And do a sort by count(bbt.id_tag) descending ...

    Threat request is not checked, possible errors.