我正在尝试为我网站上的服务器创建一个过滤器功能。用户可以为他们想要过滤的类别检查不同的选项,我有一个 ajax 请求返回满足他们条件的服务器。但是,我的 mysql_query 不工作,我想我的语法可能有误。
默认情况下,每个类别的选项设置为 1。我当前的查询是:
$order = "SELECT * FROM `servers`
WHERE `size` = '$size' or
1 = '$size' and
`type` = '$type' or
1 = '$type' and
`mode` = '$gamemode' or
1 = '$gamemode' and
`main` = '$main' or
1 = '$main'
ORDER BY $orderby
LIMIT 5";
它似乎没有得到正确的服务器,我的查询有错误吗?
谢谢!
最佳答案
在查询中混合使用 and
和 or
时,必须使用括号对条件进行分组。另外,我认为当您说 1 = '$size' 时,我认为您的意思是 `size`=1
。
$order = "SELECT * FROM `servers`
WHERE
(`size` = '$size' or `size` = '1') and
(`type` = '$type' or `type` = 1) and
(`mode` = '$gamemode' or `mode`= 1 ) and
(`main` = '$main' or `main` = 1)
ORDER BY $orderby
LIMIT 5";
关于php - 具有多个 ands 和 ors 的 Mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16766239/