选择价格在查询范围之间

时间:2017-02-28 21:21:32

标签: php sql

大家好我想知道如何选择范围之间的价格,例如: 1000 ... 2000 ... [最小(2100)...最大(4000)] ... 5000 ...

价格列在书架表中

我的查询是:



$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 categories cat
          ON cat.id = bk.category

       JOIN books_covers bk_co
          ON bk_co.book_id = bk.id

       JOIN covers co
          ON co.id = bk_co.cover_id

       JOIN books_authors bk_aut
          ON bk_aut.book_id = bk.id

       JOIN authors aut
          ON aut.id = bk_aut.author_id

       JOIN books_languages bk_lan
          ON bk_lan.book_id = bk.id

        JOIN languages lan
          ON lan.id = bk_lan.lang_id

       JOIN books_locations bk_loc
          ON bk_loc.book_id = bk.id

       JOIN locations loc 
          ON loc.id = bk_loc.location_id

          WHERE (bk.price >= 2100 AND bk.price <= 4000) OR (bk.price >= 1000 AND bk.price <= 2000)

";

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

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

      foreach($_GET as $key => $value) {
        $temp = is_array($value) ? $value : trim($value);
        if (!empty($temp)) {
          list($key) = explode("-",$key);
          if ($key == 'srch_locations'){
            array_push($locations,$value);
          }
          if (!in_array($key,$getters)){
            $getters[$key] = $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%')");
            break;
            case 'srch_category':
            array_push($queries, "bk.category = $srch_category");
            break;
            case 'srch_cover':
            array_push($queries, "bk_co.cover_id = $srch_cover");
            break;
            case 'srch_author':
            array_push($queries, "bk_aut.author_id = $srch_author");
            break;
            case 'srch_language':
            array_push($queries, "bk_lan.lang_id = $srch_language");
            break;
            case 'srch_year':
            array_push($queries, "YEAR(bk.date_released) = $srch_year");
            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";
  } 
&#13;
&#13;
&#13;

我更新了代码谢谢你提前

2 个答案:

答案 0 :(得分:1)

您可以使用WHERE bk.price BETWEEN '5000' AND '8000'

chown root:root /path/to/file:
  cmd.run

chmod 444 /path/to/file:
  cmd.run

更多信息here

答案 1 :(得分:1)

添加

WHERE (bk.price >= 2100 AND bk.price <= 4000)
如果你想要[Min(2100)... Max(4000)]

之间的价格范围,

到联接后的查询结尾

您可以使用OR

执行多个价格范围
WHERE (bk.price >= 2100 AND bk.price <= 4000)
OR    (bk.price >= 1000 AND bk.price <= 2000)