我有2张桌子: 问题:
-----------------------
active | question | qid
-----------------------
1 | "bla" | 5
1 | "bla" | 6
1 | "bla" | 7
0 | "bla" | 8
答案:
-----------------------
userid | answer | qid
-----------------------
1 | "bla" | 6
2 | "bla" | 5
userid和qid是唯一的 我希望活动的问题(问题,qid)为1,并且用户x不能对此进行回答。
我有代码:
SELECT DISTINCT questions.qid , questions.question
FROM questions
JOIN answers on (answers.userid = 1)
where questions.active = 1 and (answers.qid != questions.qid)
但它仅对用户1有好处(给我们qid:5,7)。 我想升级对用户3也有用的代码(将给我们qid:5,6,7)
谢谢
答案 0 :(得分:0)
在这种情况下,您可以使用EXISTS:
SELECT DISTINCT q.qid , q.question
FROM questions q
WHERE q.active = 1
AND NOT EXISTS (
SELECT 1 FROM answers
WHERE userid = 1 AND qid = q.qid
)
您也可以将WHERE userid = 1
更改为WHERE userid IN (1, 2, 3)
,以检查是否有多个用户。