通过最小值或最大值显示数据库中的记录

时间:2017-03-13 23:21:07

标签: php

您好我尝试使用最低或最高价格对数据库中的记录进行排序,但没有成功,因为我不知道如何在这种情况下进行分组

$sql = "SELECT p.title AS Title, p.date_published) AS Datetime , p.price   AS Price, c.name AS Category, min(p.price) AS minPrice, max(p.price) AS maxPrice FROM products p

           INNER JOIN categories c
              ON c.id = p.category
           INNER JOIN brands b
              ON b.id = p.brand
              .
              .
              .
    ";

    if (isset($_GET['search'])){

          $locations = array();
          $getters = array();
          $queries = array();

          foreach($_GET as $key => $value) {
          .
          .
          .
          }

          if (!empty($brands)) {
            $brd_q = implode(",",$brands);
          }

          if(!empty($getters)) {

            foreach($getters as $key => $value){
              ${$key} = $value;
              switch($key) {
                case 'search':
                array_push($queries, "(p.title LIKE '%$search%' || p.description LIKE '%$search%' || p.number LIKE '%$search%')");
                break;
                case 'scategory':
                array_push($queries, "p.category = $scategory");
                break;
                case 'sbrands':
                array_push($queries, "p_brd.brand_id IN ($brd_q)");
                break;
                .
                .
                .
              }
           }
        }
         
         if(!empty($queries)) {
             $sql .= " WHERE ";
             $i=1;
             foreach($queries as $query) {
                 if ($i < count($queries)) {
                     $sql .= $query." AND ";
                 }else{
                     $sql .= $query;
                 }
                 $i++;
             }

         }
        
          $sql .= " ORDER BY Datetime DESC";
      } 

如何在此查询中分组您有什么想法? 谢谢你们上次帮助我

0 个答案:

没有答案