如何将Where函数分组后进入此查询?

时间:2017-03-06 05:01:18

标签: php sql

大家好,我想按最低和最高价格对书籍进行排序,但我不知道在WHERE之后我将按功能添加组?这是查询



$sql = "SELECT DISTINCT bk.title AS Title, YEAR(bk.date_released) AS Year, bk.price   AS Price, cat.name AS Category, aut.name AS Author FROM books bk

       JOIN books_authors bk_aut
          ON bk_aut.book_id = bk.id

       JOIN authors aut
          ON aut.id = bk_aut.author_id

       JOIN books_categories bk_cat
          ON bk_cat.book_id = bk.id

        JOIN categories cat
          ON cat.id = bk_cat.cat_id  
          
          JOIN books_locations bk_loc
          ON bk_cat.book_id = bk.id

        JOIN locations loc
          ON loc.id = bk_loc.loc_id          
";

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

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

      foreach($_GET as $key => $value) {
        $temp = is_array($value) ? $value : intval(trim($value));
        if (!empty($temp)) {
          list($key) = explode("-",$key);
          if ($key == 'srch_locations'){
            array_push($locations,$value);
          }
          if (!in_array($key,$getters)){
            $getters[$key] = intval(trim($value));
          }
        }
      }

      if (!empty($locations)) {
        $loc_qry = implode(",",$locations);
      }

      if(!empty($getters)) {

        foreach($getters as $key => $value){
          ${$key} = $value;
          switch($key) {
            case 'srch_for':
            array_push($queries, "(bk.title LIKE '%$srch_for%' || bk.description LIKE '%$srch_for%' || bk.isbn LIKE '%$srch_for%')");
          
            case 'srch_author':
            array_push($queries, "bk_aut.author_id = $srch_author");
            break;
            case 'srch_language':
            array_push($queries, "bk_cat.cat_id = $srch_category");
            break;
            case 'srch_locations':
            array_push($queries, "bk_loc.location_id IN ($loc_qry)");
            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 bk.title ASC";
  } 
 Run code s
&#13;
&#13;
&#13;

我是php的初学者,我不能通过倾听来学习,因为我很聋,你们帮助我们提前谢谢你们

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
 SELECT  Title,Year, sum(Price) AS Price, Category,Author 
  FROM 
  (
  SELECT DISTINCT bk.title AS Title, YEAR(bk.date_released) AS Year, bk.price   AS Price, cat.name AS Category, aut.name AS Author 
  FROM books bk
   JOIN books_authors bk_aut
          ON bk_aut.book_id = bk.id

       JOIN authors aut
          ON aut.id = bk_aut.author_id

       JOIN books_categories bk_cat
          ON bk_cat.book_id = bk.id

        JOIN categories cat
          ON cat.id = bk_cat.cat_id  
          
          JOIN books_locations bk_loc
          ON bk_cat.book_id = bk.id

        JOIN locations loc
          ON loc.id = bk_loc.loc_id     
  )T 

  Group By Title,Year, Category,A
&#13;
&#13;
&#13;

你可以使用像这样的子查询..