我有四张桌子
Survey Question Responses Participants
============= ============== ============== =============
id_S id_Q id_R id_P
id_S id_Q id_S
id_P
id_S
Answer
我正试图找出如何检索所有回答“D”作为问题3答案的参与者的答案。
这是我的第一个怪癖,但毫不奇怪,它不起作用。
SELECT *
FROM responses r
LEFT JOIN participants p
ON r.id_P = p.id_P
LEFT JOIN responses r2
ON p.id_R = r.id_P
WHERE r.id_S = 1 AND r2.Answer = "D" AND r2.id_Q = 1
By不起作用,它返回太多记录。SQL
Select * FROM responses WHERE id_S =1
将返回1891条记录,但上面的热混乱返回15128条记录。很明显,我甚至没有正确的概念如何让它工作,这不是一个简单的语法调整是必要的。
最佳答案
SELECT ra.*
FROM responses rd
JOIN responses ra
ON (ra.id_s, ra.id_p) = (rd.id_s, rd.id_p)
WHERE (rd.id_q, rd.answer) = (3, 'D')
关于mysql - 链接联接回到目标表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24323094/