当我应用过滤器查询不起作用时,如何知道 Where 子句已经存在,如果存在,那么接下来用 AND 替换哪里?

When I apply filter query not working and how to know the Where clause is already exist if exist so next replace where with AND?

查询 1:AND (installation.InstallationStatus='0') 查询 2:AND (installation.active='1')

当我创建一个过滤器并同时应用查询 1 和查询 2 时,查询构建类似这样的查询:SELECT * FROM orders WHERE AND (installation.active='1') AND (installation.InstallationStatus='0')

但是我想要这个 查询:SELECT * FROM orders WHERE (installation.active='1') AND (installation.InstallationStatus='0');

php代码在这里

            ```
            //Filter By installStatus
            if (isset($_SESSION['filter']['installStatus']) && !empty($_SESSION['filter']['installStatus'])) {
                $FilterInstallStatus ="AND (installation.InstallationStatus='".$_SESSION['filter']['installStatus']."')";
            } else {
                $FilterInstallStatus = "";
            }


            //Filter By Active
            if (isset($_SESSION['filter']['active']) && !empty($_SESSION['filter']['active'])) {
                $FilterActive ="AND (installation.active='".$_SESSION['filter']['active']."')";
            } else {
                $FilterActive = "";
            }

            $allrecords = $connection->query("(SELECT orders.*,installation.* FROM orders LEFT JOIN installation ON orders.OrderId = installation.OrderId WHERE".$FilterCreationDate." ".$FilterDateFull." ".$FilterModelName." ".$FilterInstallStatus." ".$FilterActive." ".$FilterUserFilter." ".$FilterLastUpdate." GROUP BY orders.OrderId) UNION (SELECT orders.*,installation.* FROM orders RIGHT JOIN installation ON orders.OrderId = installation.OrderId WHERE".$FilterCreationDate." ".$FilterDateFull." ".$FilterModelName." ".$FilterInstallStatus." ".$FilterActive." ".$FilterUserFilter." ".$FilterLastUpdate." GROUP BY orders.OrderId) ORDER BY active DESC, CreationDate DESC, lastUpdate DESC, brandStatus DESC LIMIT $start_from, $record_per_page");

            ```

您应该以不同的方式构建查询。像这样:

  $filter_query = '';

  //Filter By installStatus
        if (isset($_SESSION['filter']['installStatus']) && !empty($_SESSION['filter']['installStatus'])) {
            $filter_query = "(installation.InstallationStatus='".$_SESSION['filter']['installStatus']."')";
        }

        //Filter By Active
        if (isset($_SESSION['filter']['active']) && !empty($_SESSION['filter']['active'])) {
            if ($filter_query != '')
                $filter_query .= ' AND ';

            $filter_query  .= "(installation.active='".$_SESSION['filter']['active']."')";
        }

// here all other filters conditions with check if $filter_query is not empty
// and finally db query

$allrecords = $connection->query("(SELECT orders.*,installation.* FROM orders LEFT JOIN installation ON orders.OrderId = installation.OrderId ".($filter_query !='' ? "WHERE ".$filter_query : "")." GROUP BY orders.OrderId) ORDER BY active DESC, CreationDate DESC, lastUpdate DESC, brandStatus DESC LIMIT $start_from, $record_per_page");

您可以将过滤器放在一个数组中,然后使用 AND:

加入它们
$filter = array();

//Filter By installStatus
if (!empty($_SESSION['filter']['installStatus'])) {
    $filte[] = "(installation.InstallationStatus='".$_SESSION['filter']['installStatus']."')";
}

//Filter By Active
if ( !empty($_SESSION['filter']['active'])) {
    $filter[] = "(installation.active='".$_SESSION['filter']['active']."')";
}

// here all other filters conditions with check if $filter is not empty
// and finally db query
$where = !empty($filter) ? implode(' AND ', $filter) : '';

$allrecords = $connection->query("(SELECT orders.*,installation.* FROM orders LEFT JOIN installation ON orders.OrderId = installation.OrderId ".($filter_query !='' ? "WHERE ".$where : "")." GROUP BY orders.OrderId) ORDER BY active DESC, CreationDate DESC, lastUpdate DESC, brandStatus DESC LIMIT $start_from, $record_per_page");
  1. 此方法允许您向查询添加任意数量的过滤器,只需向 $filter 数组添加一个新元素即可。
  2. 无需在相同的if条件下同时使用isset()!empty()!empty()就足够了。