我有两张桌子:
question
question_id | question
question_answer
answer_id | question_id | choice_id | user_id | explain
我想得到所有特定用户还没有回答的问题。
SELECT question, question_id as questionId
FROM question q
LEFT JOIN question_answer qa USING(question_id)
WHERE qa.user_id!=$userId
在这种情况下,我得到零行。我也试过了
SELECT question, q.question_id as questionId
FROM question q
LEFT JOIN question_answer qa ON q.question_id=qa.question_id AND qa.user_id!=$userId
显然这会返回所有记录。我一直在问这个问题。
最佳答案
检查特定用户的LEFT JOIN
部分应该颠倒,即user_id = $userId
而不是user_id <> $userId
。
从结果行中选择user_id IS NULL
的行,以查找$userId
尚未回答的问题。
SELECT question, q.question_id as questionId
FROM question q
LEFT JOIN question_answer qa ON q.question_id=qa.question_id AND qa.user_id=$userId
WHERE qa.user_id IS NULL
关于mysql - 左联接返回零行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13399048/