Sqlfiddle here: http://sqlfiddle.com/#!2/4c532/6
I have a table (user_answers
) that holds the answers that users give to questions. In the sqlfiddle we have data for user 1 and for user 2. User 1 has answered 5 questions total and user 2 has answered 8 questions total.
The result I am looking for:
I want to be able to pull how many questions a user has answered that the other one has also answered. An example output would be like this: "User 1 has answered 5 out of the 8 questions you have answered".
I have tried joining the tables but it is giving me more results than I was expecting and I hadn't yet specified which users to test in this query:
SELECT * FROM user_answers t1
JOIN user_answers t2 ON t1.uid > t2.uid
AND t1.answer IS NOT NULL
And then when I tried to specify users I got no results with this query:
SELECT * FROM user_answers t1
JOIN user_answers t2 ON t1.uid > t2.uid
AND t1.answer IS NOT NULL
AND t1.uid = 1
AND t2.uid = 2
I feel like I'm getting closer but could use some guidance.
Thanks.