php - 多重条件和 id 检查

标签 php mysql where-clause conditional-statements

嗨,我有一个 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/

相关文章:

mysql - 优化性能不佳的查询

mysql子查询where和group by

Swift - 仅为其中一个案例扩展枚举

php - 如何为 Laravel 5.2 Eloquent 模型编写 "on duplicate key update"?

php - 允许用户对评论进行一次评分 PHP MySQL

php 扩展 >> 错误

mysql - "where"或 "like"子句更适合使用索引

php - MySQLi 准备好的语句未采用调用过程的有效查询

使用索引时 MySQL 查询会变慢

php - 无法从 MySQL 获取数据 - PHP