2

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

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

谢谢!

4

5 回答 5

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";
于 2013-05-27T04:32:03.983 回答
2
"SELECT * FROM `servers` WHERE 
`size` in ('$size', 1) and 
`type` in( '$type' ,1) and 
`mode` in('$gamemode' ,1) and 
`main` in ( '$main' , 1 ) 
ORDER BY $orderby LIMIT 5";
于 2013-05-27T04:45:05.900 回答
1

您需要添加括号,因为or并且and具有不同的操作顺序并且需要括号以允许您需要完成的事情

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
于 2013-05-27T04:32:16.750 回答
1

您必须使用方括号才能在查询中使用多个 OR 条件

mysql_query("SELECT * FROM servers WHERE email='$Email' AND (date='$Date_Today' OR date='$Date_Yesterday' OR date='$Date_TwoDaysAgo' OR date='$Date_ThreeDaysAgo' OR date='$Date_FourDaysAgo')");

你可以改变你的列名

你也可以使用 IN 条件

mysql_query("SELECT * FROM servers WHERE email='$Email' AND date IN ('$Date_Today','$Date_Yesterday','$Date_TwoDaysAgo')");

如果我能帮助你更多,请告诉我

于 2013-05-27T04:34:14.443 回答
0

试着在or条件周围加上支架

$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";
于 2013-05-27T04:33:18.343 回答