2017-03-02 73 views
1

您好人我有這個數據庫體系結構:如何添加最小和最大的價格在此查詢

table books [id,title,description,date_released] 
 
------------------------------------------------------- 
 
table authors [id,name] 
 
table books_authors[book_id,author_id] 
 
ALTER TABLE `books_authors` 
 
    ADD CONSTRAINT `books_authors_ibfk_1` FOREIGN KEY (`book_id`) REFERENCES `books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, 
 
    ADD CONSTRAINT `books_authors_ibfk_2` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; 
 
-------------------------------------------------------- 
 
table categories [id,name] 
 
table books_authots[book_id,category_id] 
 
ALTER TABLE `books_categories` 
 
    ADD CONSTRAINT `books_categories_ibfk_1` FOREIGN KEY (`book_id`) REFERENCES `books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, 
 
    ADD CONSTRAINT `books_categories_ibfk_2` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; 
 
-------------------------------------------------------- 
 
table books_locations [id,name] 
 
table books_locations [book_id,location_id] 
 
ALTER TABLE `books_locations` 
 
    ADD CONSTRAINT `books_locations_ibfk_1` FOREIGN KEY (`book_id`) REFERENCES `books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, 
 
    ADD CONSTRAINT `books_locations_ibfk_2` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

查詢是:

$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"; 
 
    }

我想要t按Min(價格)和Max(價格)對書籍進行排序是否正確?

$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), min(price), max(price) FROM books bk .....";

和其中i由組? 你可以幫我請謝謝你提前

回答

0

它看起來像你需要像這樣(沒有distinct需要):

select 
    bk.title as Title 
    , year(bk.date_released) as Year 
    , pr.price as Price 
    , cat.name as Category 
    , aut.name as Author 
    , min(price) as minPrice 
    , max(price) as maxPrice 
from books bk 
    inner join books_categories bkcat 
    on bk.book_id = bkcat.book_id 
    inner join categories cat 
    on bkcat.cat_id = cat.cat_id 
    inner join books_authors bkaut 
    on bk.book_id = bkaut.book_id 
    inner join authors aut 
    on bkaut.author_id = aut.author_id 
group by 
    bk.title 
    , year(bk.date_released) 
    , pr.price 
    , cat.name 
    , aut.name 
+0

謝謝sqlZim有沒有在代碼中的一些拼寫錯誤上面我更新了它謝謝你這麼多爲你的答案 –

+0

@AlexSoprano更新答案代碼,以反映表名更改 – SqlZim

+0

我得到這個錯誤你的SQL語法有錯誤;檢查與你的MySQL服務器版本相對應的手冊,在'WHERE(bk.title LIKE'%1%'|| bk.description LIKE'%1%')ORDER BY bk.title ASC' 45 –

相關問題