使用“OR”和“AND”mysql php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5678413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:10:37  来源:igfitidea点击:

using "OR" and "AND" mysql php

phpmysql

提问by Dan Stern

I have a MySQL query, where I want to get a value, lets say mthat can be 'x', 'y' or 'z' and two other variables which need to be exact values. I don't know how to write this.

我有一个 MySQL 查询,我想在其中获取一个值,可以说它m可以是 'x'、'y' 或 'z' 以及需要精确值的其他两个变量。我不知道怎么写这个。

I don't know if you get the idea, but this is what I have:

我不知道你是否明白,但这就是我的想法:

SELECT * FROM respuestas 
WHERE id_curso = '155' OR id_curso = '156' OR id_curso= '157' OR id_curso= '158' 
AND id_pregunta='1' AND respuesta>=0

So I need id_cursoto be 155or 156or 157or 158and at the same time, id_pregunta= 1and respuesta> 0.

所以我需要id_curso同时是155or156157or 158id_pregunta=1respuesta> 0

I know this is not the way to write it, but I don't know the right way.

我知道这不是写它的方式,但我不知道正确的方式。

回答by Emil Vikstr?m

Try this:

尝试这个:

WHERE
  id_curso IN('155', '156', '157', '158') AND
  id_pregunta = '1' AND
  respuesta >= 0

Or, if you prefer your verbose way, use a parenthesis:

或者,如果您更喜欢冗长的方式,请使用括号:

WHERE
  (id_curso = '155' OR
   id_curso = '156' OR
   id_curso = '157' OR
   id_curso = '158') AND
  id_pregunta = '1' AND
  respuesta >= 0

If id_curso is an integer (which it doesn't seem to be by your query since you've used quotation marks, but here goes anyway), you can even use the BETWEEN keyword to select a range of values:

如果 id_curso 是一个整数(它似乎不是您的查询,因为您使用了引号,但无论如何都在这里),您甚至可以使用 BETWEEN 关键字来选择一系列值:

WHERE
  (id_curso BETWEEN 155 AND 158) AND
  id_pregunta = '1' AND
  respuesta >= 0

回答by jeroen

You need to group your ORstatements so that they form one condition:

您需要对OR语句进行分组,以便它们形成一个条件:

SELECT * FROM respuestas WHERE (id_curso= '155' OR id_curso= '156' OR id_curso= '157' OR id_curso= '158') AND id_pregunta='1' AND respuesta>=0

回答by Jan Zyka

AND has higher priority than OR, you need to use brackets:

AND 的优先级高于 OR,需要使用括号:

SELECT * FROM respuestas WHERE (id_curso= '155' OR id_curso= '156' OR id_curso= '157' OR id_curso= '158') AND id_pregunta='1' AND respuesta>=0 

回答by blasteralfred Ψ

Try grouping

尝试分组

SELECT * FROM respuestas WHERE (id_curso= '155' OR id_curso= '156' OR id_curso= '157' OR id_curso= '158') AND id_pregunta='1' AND respuesta>=0

SELECT * FROM respuestas WHERE (id_curso= '155' OR id_curso= '156' OR id_curso= '157' OR id_curso= '158') AND id_pregunta='1' AND respuesta>=0

回答by Benjamin A

You could try something like this...

你可以试试这样的...

SELECT * FROM respuestas WHERE id_curso IN ('155','156','157','158') AND id_pregunta = '1' AND respuesta >= 0

if you don't want to use IN you can also do this...

如果你不想使用 IN 你也可以这样做......

SELECT * FROM respuestas WHERE (id_curso = '155' OR id_curso = '156' OR id_curso = '157' OR id_curso = '158') AND id_pregunta = '1' AND respuesta >= 0

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

回答by Dongsheng Cai

you should use IN Operator

你应该使用 IN 运算符

SELECT * FROM respuestas WHERE id_curso IN (155, 156, 157, 158) AND id_pregunta='1' AND respuesta>=0