I have two tables
Topic
----------------------
id title
----------------------
1 Loops
2 Control Structure
3 Basics
Chapters
------------------------------------------------------
id book_id topic_id chapter_number
------------------------------------------------------
1 1 1 1
2 1 2 2
3 1 3 3
4 2 1 5
5 2 3 2
6 3 3 1
When searched for topics, I want to show book_ids that has chapters on those topics.
Book_ids that have topic 1 and 2: 1
Book_Ids that have topic 3: 1,2,3
BookIds that have topic 1 and 3: 1
This is my query:
SELECT DISTINCT chapter.book_id FROM chapter Inner Join topic ON topic.id = chapter.topic_id WHERE topic.id IN ( '1,3');
but it return Book_ids of : 1 and 3 while it should only return 1. It seems my query returns union while it should return an intersection.
Can someone help me fixing it?
Here is SQLFiddle demo. Just replace
'1,3'in this query to any set of chapters you need: