This question has already been answered:
There are 2 tables User, Book, one-to-many relationship, that is, the user can have many books.
* Book
- user_id
- book_id
From the structure of the table it can be seen that the relationship is of the type many_many, but this is not so important. For example, I need to select users who have read books with ID 1 and 2.
Такой запрос не работает "SELECT * FROM user LEFT JOIN book ON user.id = book.user_id WHERE book.book_id = 1 AND book.book_id = 2 ";
book.book_id = 1 AND book.book_id = 2
always false. One entry cannot haveid=1 И-одновременно id=2
. It would be correct tobook.book_id = 1 OR book.book_id = 2
, which in a short record looks likebook.book_id IN(1,2)
- Mikebook.book_id = 1 AND book.book_id = 2
- I understand that this condition is false, but I don’t know how to compose a query correctly - Roman Maltsevнужно выбрать пользователей которые прочли книги с ИД 1 и 2
. If you put an OR - it will not give the desired result. - Roman Maltsev