我有两张表用于多项选择问卷(每个用户回答一系列问题):
users (userID, name, email)
votes (voteID, userID, questionID, answerID)
样本数据(用户):
0, Some Name, some@thing.com
1, Other Name, some@one.com
样本数据(票数):
0, 1, 1, 1
1, 1, 2, 2
2, 1, 3, 2
我想选择所有有正确答案的用户。
我试过这个(我已经硬编码了答案):
$sql = "SELECT users.userID, users.name, users.email FROM users
INNER JOIN votes ON (users.userID = votes.userID)
WHERE (votes.questionID = '1' AND votes.answerID = '1')
AND (votes.questionID = '2' AND votes.answerID = '2')
AND (votes.questionID = '3' AND votes.answerID = '2')
AND (votes.questionID = '4' AND votes.answerID = '3')
AND (votes.questionID = '5' AND votes.answerID = '1')
GROUP BY users.userID";
但这不会返回任何东西。
我也尝试过这样的事情(我也硬编码了答案):
$sql = "SELECT users.userID, users.name, users.email FROM users
INNER JOIN transfertipsvotes ON (users.userID = transfertipsvotes.userID)
WHERE (transfertipsvotes.questionID = '1' AND transfertipsvotes.answerID = '1') GROUP BY users.userID
UNION
SELECT users.userID, users.name, users.email FROM users
INNER JOIN transfertipsvotes ON (users.userID = transfertipsvotes.userID)
WHERE (transfertipsvotes.questionID = '2' AND transfertipsvotes.answerID = '2') GROUP BY users.userID
UNION
SELECT users.userID, users.name, users.email FROM users
INNER JOIN transfertipsvotes ON (users.userID = transfertipsvotes.userID)
WHERE (transfertipsvotes.questionID = '3' AND transfertipsvotes.answerID = '2') GROUP BY users.userID";
但这只会为所有用户返回一个正确答案。
如何进行正确的查询以选择所有具有正确答案的用户?