动态分组查询的AND和OR部分

时间:2015-04-10 06:50:37

标签: php mysql

所以我有这个:

$query = "SELECT personaname, profileurl, avatarmedium, location, country, rank, rankurl, role, map, skype, teamspeak, microphone, esea, faceit, cevo, competitive FROM csgolistings WHERE active = 1";

if(isset($_POST['location']) && $_POST['location'] != "") {
  $location = $_POST['location'];
  $query .= " AND location = '$location'"; 
}

if(isset($_POST['country']) && $_POST['country'] != "") {
  $country = $_POST['country'];
  $query .= " AND country = '$country'"; 
}

if(isset($_POST['rank']) && $_POST['rank'] != "") {
  $location = $_POST['rank'];
  $query .= " AND rank = '$rank'"; 
}

if(isset($_POST['competitive']) && $_POST['competitive'] != "") {
  $query .= " AND competitive = 1"; 
}

if(isset($_POST['cevo']) && $_POST['cevo'] != "") {
  $query .= " AND cevo = 1"; 
}

if(isset($_POST['faceit']) && $_POST['faceit'] != "") {
  $query .= " AND faceit = 1"; 
}

if(isset($_POST['esea']) && $_POST['esea'] != "") {
  $query .= " AND esea = 1"; 
}

if(isset($_POST['microphone']) && $_POST['microphone'] != "") {
  $query .= " AND microphone = 1"; 
}

if(isset($_POST['teamspeak']) && $_POST['teamspeak'] != "") {
  $query .= " AND teamspeak = 1"; 
}

if(isset($_POST['skype']) && $_POST['skype'] != "") {
  $query .= " AND skype = 1"; 
}

if(isset($_POST['role']) && $_POST['role'] != "") {
  $role = $_POST['role'];
  $query .= " AND role = '$role'"; 
}

if(isset($_POST['map']) && $_POST['map'] != "") {
  $map = $_POST['map'];
  $query .= " AND map = '$map'"; 
}

所以我最终得到$query就是这样(取决于POST的搜索过滤器:

SELECT personaname, profileurl, avatarmedium, location, country, rank, rankurl, role, 
map, skype, teamspeak, microphone, esea, faceit, cevo, competitive FROM csgolistings WHERE active = 1 AND rank = '$rank' AND cevo = 1

我的问题是,如何让它将位置和国家分组为AND,然后其余的东西是OR?我需要在OR周围放置括号,以便它找到来自位置和国家的所有人AND(cevo = 1,rank ='$ rank')。我可以在平面查询中轻松完成,但我正在处理$ _POST数据,其中一些可能不会被发布。这取决于用户请求的内容。

希望这是有道理的。 任何帮助是极大的赞赏。 谢谢!

3 个答案:

答案 0 :(得分:4)

你可以试试这个:

<?php
   $required = array ('location', 'country');
   $optional = array ('microphone', 'faceit', ... );

   $ands = array( '1' => '1');

   foreach($required as $req)
   {
      if(isset($_POST[$req]) && $_POST[$req] != '')
      {
        $ands[$req] = "$req = '{$_POST[$req]}'";
      }
   }

   $ors = array();

   foreach($optional as $req)
   {
      if(isset($_POST[$req]) && $_POST[$req] != '')
      {
        $ors[$req] = "$req = '{$_POST[$req]}'";
      }
   }

   $and_part = implode(" AND ", $ands);
   $or_part = empty($ors) ? "1" : implode(" OR ", $ors);

   $query = "select .... from ... WHERE $and_part AND ($or_part) ... ";
?>

您可以通过将参数添加到顶部声明的相应数组来轻松扩展查询,而无需添加到巨型if链。

给出POST输入:

   $_POST = array(
     'location' => 'city',
     'country' => 'antarctica',
     'microphone' => 'loud',
     'faceit' => 'yes'
   );

$query将包含select .... from ... WHERE 1 AND location = 'city' AND country = 'antarctica' AND (microphone = 'loud' OR faceit = 'yes') ...

答案 1 :(得分:1)

首先,在处理需要通过分隔符加入的字符串时,它可能是最简单的(可以是&#34;,&#34;到&#34; AND&#34; )创建一个array来保存它们,然后使用join($delim, $array)函数生成一个新字符串,每个旧字符串之间都有分隔符。您可以在PHP手册中查看join的文档。

其次,我的方法如下:尝试获取国家,并尝试获取位置。如果无法获取(因为它们不在POST中),请将相应的变量设置为null。

然后将所有其余字符串收集到一个数组中,比如说$conditions,省略&#34; AND&#34;或&#34;,&#34;或者诸如此类的,因为我们稍后会有条件地添加适当的一个。这是一个例子:

if(isset($_POST['esea']) && $_POST['esea'] != "") {
    $conditions[] = "esea = 1"; 
}

然后:如果有位置和国家(即两者都设置为非空值),则构造查询:

$query .= "location = '$location' AND country = '$country' AND (" . join(",", $conditions) . ")"; 

否则,将任何位置或国家/地区与由&#34;加入的$条件组合在一起。 AND&#34;,像这样:

if ($location) { 
    $conditions[] = "location = '$location'";
}
if ($country) {
    $conditions[] = "country = '$country'";
}
$query .= join(" AND ", $conditions);

希望这会有所帮助。

答案 2 :(得分:0)

如果我没错,你希望得到你的疑问,

where active =1 and (location = '$location' and country = '$country') or (rank = '$rank' etc etc )

嗯,如果是这样的话,你可以简单地将你的位置和国家/地区串联起来If and else IF statements,就像这样

if((isset($_POST['location']) && $_POST['location'] != "") && (isset($_POST['country']) && $_POST['country'] != "") ) {
  $location = $_POST['location'];
  $country = $_POST['country'];
  $query .= " AND (location = '$location' and country = '$country')"; 
}
else if(isset($_POST['country']) && $_POST['country'] != "") {
  $country = $_POST['country'];
  $query .= " AND (country = '$country')"; 
}
else if(isset($_POST['location']) && $_POST['location'] != "")
{
   $location = $_POST['location'];
   $query .= " AND (location = '$location')"; 
}

然后,你可以继续与你进行正常的接触,如果与OR一起使用

$query .= "OR ( ";
if(isset($_POST['rank']) && $_POST['rank'] != "") {
  $location = $_POST['rank'];
  $query .= " rank = '$rank'"; 
}

if(isset($_POST['competitive']) && $_POST['competitive'] != "") {
  $query .= " And competitive = 1"; 
}

if(isset($_POST['cevo']) && $_POST['cevo'] != "") {
  $query .= " AND cevo = 1"; 
}

if(isset($_POST['faceit']) && $_POST['faceit'] != "") {
  $query .= " AND faceit = 1"; 
}

if(isset($_POST['esea']) && $_POST['esea'] != "") {
  $query .= " AND esea = 1"; 
}

if(isset($_POST['microphone']) && $_POST['microphone'] != "") {
  $query .= " AND microphone = 1"; 
}

$query.= ")";

你知道这种事。你按照你的要求做组合。你也可以使用不同的变量来实现eacj&#34;和&#34;和&#34;或&#34;部分,然后将它们连接在一起。根据我的建议,这会更好。希望这可以帮助。