So I have two tables. One is questions table and another one is the answers table with the below mentioned schema.
Question:
id title category_id active
Answer:
id question_id player_id active
Basically, what I need is a query to get the number of questions remaining grouped together for each player for a particular category_id
SELECT * FROM questions WHERE id NOT IN (SELECT DISTINCT(s.question_id) FROM answers s)
It returns me the remaining questions, but its not grouped together for particular player and if it is joined with the same answers table, then the result set is empty.
Query that I have tried:
SELECT g.player_id, COUNT(questions.question_id) as Count
FROM `questions` JOIN answers g ON g.id = questions.id
WHERE g.question_id NOT IN
(
SELECT DISTINCT(question_id)
FROM answers
) GROUP BY g.player_id
I have already got the desired result using PHP and used multiple loops to get the required set and it is not optimized solution and takes a lot of time. So, want to solve it by writing a proper query.
Thanks for your time and appreciate if anyone can guide me.
Table Data:
Questions:
id title category_id active
1 A 1 1
2 B 1 1
3 C 1 1
4 D 1 1
5 E 1 1
Answers:
id question_id player_id active
1 1 1 1
1 3 1 1
1 2 2 1
1 4 3 1
Expected Output:
player_id Count_of_Remaining_Questions Category ID
1 3 1
2 4 1
3 4 1