最好在我的例子中显示:
我有桌子,存储用户的答案来自一个大表格。每个表格有139个问题。这些问题存储在不同的表中,需要时使用questionID连接。对于每个用户,都有一个ID。我现在需要制作过滤器,仅显示与特定问题匹配的用户或更多答案的用户。
例如,我想要用户,问题14回答“是”,问题54不为空,问题100大于10.这就是表格的外观:
**userID** | **questionID** | **answer**
1 14 "yes"
1 54 "something"
1 100 "9"
2 14 "no"
2 54 "north
2 100 "50"
3 14 "yes"
3 54 "test"
3 100 "12"
结果我只想返回 userID 3,因为它符合所有条件。
使用ColdFusion很容易实现,因为它允许在查询结果中查询,但在PHP中我没有找到任何方法。重要的是有机会添加随机数量的问题,而不仅仅是这个例子中的三个。
答案 0 :(得分:7)
尝试,
SELECT userID
FROM tableName
WHERE (questionID = 14 AND
answer = 'yes' ) OR
(questionID = 54 AND
answer <> 'empty') OR
(questionid = 100 AND
answer > 10)
GROUP BY userID
HAVING COUNT(*) = 3
答案 1 :(得分:4)
SELECT q.userID
FROM questions q
JOIN questions qq ON qq.userID=q.userID
AND qq.questionID='54' AND qq.answer IS NOT NULL
JOIN questions qqq ON qqq.userID=q.userID
AND qqq.questionID='100' AND qqq.answer > 10
WHERE q.questionID=14
AND q.answer = 'yes'
答案 2 :(得分:1)
您可以尝试:
SELECT questionID, answer
FROM table
WHERE ( questionID = 14 AND answer = 'yes' )
OR ( questionID = 54 AND answer != '' )
OR ( questionID = 100 AND answer > 10 );