我在 mysql 中有 3 个表,1 个用于用户,2 个用于问题,3 个用于结果。
我编写了以下查询。
SELECT q.id,qr.user_id
FROM questions AS q
LEFT JOIN results AS r ON q.id=r.question_id
and r.user_id in (SELECT user_id FROM students)
如果该问题的结果中不存在答案。我想要结果中包含问题和用户 ID。 示例
user
U1
U2
U3
question
A
B
C
result
A U1 YES
A U2 YES
A U3 YES
B U1 YES
B U2 YES
B U3 YES
C U2 YES
C U3 YES
Desired Output
A U1 YES
A U2 YES
A U3 YES
B U1 YES
B U2 YES
B U3 YES
**C U1 No**
C U2 YES
C U3 YES
请帮忙。
最佳答案
试试这个,
SELECT q.id,qr.user_id
FROM results AS r
LEFT JOIN questions AS q ON r.question_id = q.id
INNER JOIN students AS s ON r.user_id = s.user_id
关于mysql - 想要左连接选择中匹配 id 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55491463/