Mysql 如何在同一列上设置多个搜索条件

Mysql 如何在同一列上设置多个搜索条件,mysql,sql,Mysql,Sql,如果可能的话,我试图从我的数据库中得到一个“where”复合词的结果 我的表格答案如下所示: id | user_id | question_id | answers ------------------------------------ 1 | 111 | 123 | meat 2 | 111 | 124 | good 3 | 111 | 125 | 8 4 | 112 | 123 | s

如果可能的话,我试图从我的数据库中得到一个“where”复合词的结果

我的表格
答案
如下所示:

id | user_id | question_id | answers
------------------------------------
1  |   111   |     123     | meat
2  |   111   |     124     | good
3  |   111   |     125     | 8
4  |   112   |     123     | salad
5  |   112   |     124     | bad
6  |   112   |     125     | 3
7  |   113   |     123     | meat
8  |   113   |     124     | good
9  |   113   |     125     | 10
我试着用“where in”,但它不能解决整个问题。 我也尝试过这样的代码,但不起作用。始终返回
0

SELECT COUNT(DISTINCT(answers.user_id)) FROM answers
WHERE (answers.question_id = '123' AND answers.answer = 'meat')
AND (answers.question_id = '124' AND answers.answer = 'good')
AND (answers.question_id = '125' AND answers.answer > 7);
结果应该是“2”,因为有2个用户id满足所有要求。

您应该在外部条件下使用

WHERE (answers.question_id = '123' AND answers.answer = 'meat')
OR (answers.question_id = '124' AND answers.answer = 'good')
OR (answers.question_id = '125' AND answers.answer > 7);

按照您的方式,
question\u id
必须同时等于123124125!w这是不可能的:)因此,您得到了错误的结果。

您可能需要
而不是
,条件是:
(..)或(..)
Hi-Michal。非常感谢您的回复。如果我需要计数仅在所有条件都匹配时返回,该怎么办。这就是为什么我用“AND”代替“OR”。在我的示例中,应该返回“2”,但假设“ID”为9(最后一个)的行包含数字“3”,在这种情况下,我希望“COUNT”为1,但带有“OR”,则会给出2。这就像我希望将此条件-->分组,其中(answers.question_ID='123'和answers.answer='meat')