Unique movies by UNION the results of two join, MYSQL

66 Views Asked by At

I ran the following query to UNION the result of two joins over the below 4 tables:

SELECT member_CROWD.worker_Id, member_CROWD.Unreliable, Answer_CROWD.qId, Answer_CROWD.answer
FROM member_CROWD
JOIN `Answer_CROWD` 
ON member_CROWD.worker_Id = Answer_CROWD.worker_Id 
WHERE member_CROWD.Unreliable = '0' AND Answer_CROWD.qId = 'q8'
GROUP BY Answer_CROWD.answer

UNION

SELECT member.mem_id, member.Unreliable, Answer.qId, Answer.answer
FROM member
JOIN `Answer` 
ON member.mem_id = Answer.userId 
WHERE member.Unreliable = '0' AND Answer.qId = 'q8'
GROUP BY Answer.answer

This is because I have two experiments to collect data; one is through asking my friends to fill my survey (data stored in member and Answer tables), and the other one is through Crowdsourcing (data stored in member_CROWD and Answer_CROWD).

Now the problem is that, I would like to know for example "Unique Number of Movies selected by users", but from all users (friends and also Crowd). The query above could UNION the two join results, BUT still answers are not unique (because they are unique ONLY in each join result).

Could someone help me know how to get Unique #of movies in the whole experiment?

Thanks,

1

There are 1 best solutions below

3
On BEST ANSWER

Distinct can be run inside a count in mysql which is pretty handy.

select Count(Distinct answer) from (
SELECT member_CROWD.worker_Id, member_CROWD.Unreliable, Answer_CROWD.qId, Answer_CROWD.answer
FROM member_CROWD
JOIN `Answer_CROWD` 
ON member_CROWD.worker_Id = Answer_CROWD.worker_Id 
WHERE member_CROWD.Unreliable = '0' AND Answer_CROWD.qId = 'q8'
GROUP BY Answer_CROWD.answer

UNION

SELECT member.mem_id, member.Unreliable, Answer.qId, Answer.answer
FROM member
JOIN `Answer` 
ON member.mem_id = Answer.userId 
WHERE member.Unreliable = '0' AND Answer.qId = 'q8'
GROUP BY Answer.answer
) a