嗨,我有一个 mysql 查询,需要检查多个条件,因此我使用 () 来分隔每个条件,例如:
WHERE (a='string' AND b='string') OR (a='otherstring' AND b='otherstring)
我遇到的问题是我必须添加用户检查,例如我不知道是否必须再次使用 (),这是正确的吗?
WHERE ((a='string' AND b='string') OR (a='otherstring' AND b='otherstring)) AND (id='1')
谢谢!
最佳答案
WHERE
(
(a='string' AND b='string')
OR (a='otherstring' AND b='otherstring')
)
AND id='1'
这将查找 id 为 1 且 a.string=b.string 或 a.string=otherstring 的所有记录。
编辑:仅供额外说明:
基本上,括号内的任何内容都等同在一起,例如:
// Condition 1
(
this=somethingelse
and this2=somethingelse2
)
// Condition 2
and
(
someCondition=anotherConidition
and
// Condition 3
(
thirdCondition=anotherCondition3
or thirdCondition2=anotherCondition4
)
)
在此示例中。条件 1 中的两个条款都必须满足。还必须满足条件 2,并且在该条件中,someCondition 必须等于 anotherCondition
以及条件 3 中的任一语句。
关于php - 多重条件和 id 检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11394460/