php - 具有多个 ands 和 ors 的 Mysql 查询

标签 php mysql

我正在尝试为我网站上的服务器创建一个过滤器功能。用户可以为他们想要过滤的类别检查不同的选项,我有一个 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";

它似乎没有得到正确的服务器,我的查询有错误吗?

谢谢!

最佳答案

在查询中混合使用 andor 时,必须使用括号对条件进行分组。另外,我认为当您说 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/

相关文章:

mysql - 尝试通过命令行导入 .sql 时 root 用户的访问被拒绝 (Vagrant)

mysql - 在Mysql中选择列就像多个值的数组

c# - 使用 SimpleMembership (MySQL) 将用户添加到角色时出现外键问题

php - 为什么这个 PDO 语句会默默地失败?

php - 匹配mysql中的日期并给出结果

php - 如何解决 Laravel 安装上 MAC OS 上的 curl 证书问题?

MySQL如何先对子组求和,然后对总和求和

php - JQuery 周日历问题

phpcurl内存使用情况

mysql - 在具有关系数据库和 CMS 的网站上进行协作