大家好,我有这个数据库架构:
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";
}
我想按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 .....";
和我分组的地方? 能帮到我吗,谢谢你提前
答案 0 :(得分: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