我有一个图书搜索正在搜索我想要搜索的图书,这些图书可以搜索共享作者类别或发布者的图书。我有它设置,它适用于OR条款,例如具有与他们相关的类别CHILDRENS或HISTORY的书籍,这可以正常工作,但是当我搜索属于2类别(AND)的书籍时,例如CHILDRENS AND MAGIC(Harry Potter)它即使它们在数据库中链接,也不显示这些书。
以上是我使用OR进行的搜索,当我搜索属于儿童和历史的书籍时
上面你可以看到,当Harrypotter的书籍属于这两本书时,我得不到分享儿童和魔法书籍的结果。 上面是数据库中的链接,它为每本书分类,Magic是Category 1,Childrens是Category 2,你可以看到他们都分享这些。
以下是查询的PHP代码
<?php
include 'header.php';
include 'searchscriptTEST.php';
$sql = "SELECT DISTINCT bk.title AS Title, bk.bookid AS BookID, bk.year AS Year, bk.publisher AS Publisher, aut.authorname AS Author
FROM book bk
JOIN book_category bk_cat
ON bk_cat.book_id = bk.bookid
JOIN categories cat
ON cat.id = bk_cat.category_id
JOIN books_authors bk_aut
ON bk_aut.book_id = bk.bookid
JOIN authors aut
ON aut.id = bk_aut.author_id";
if(isset($_GET['searchInput'])){
$input = $_GET['searchInput'];
$input = preg_replace('/[^A-Za-z0-9]/', '', $input);
}
if (isset($input)){
$getters = array();
$queries = array();
foreach ($_GET as $key => $value) {
$temp = is_array($value) ? $value : trim($value);
if (!empty($temp)){
if (!in_array($key, $getters)){
$getters[$key] = $value;
}
}
}
if (!empty($getters)) {
foreach($getters as $key => $value){
//${$key} = $value;
switch ($key) {
case 'searchInput':
array_push($queries,"(bk.title LIKE '%{$getters['searchInput']}%'
|| bk.description LIKE '%{$getters['searchInput']}%' || bk.isbn LIKE '%{$getters['searchInput']}%'
|| bk.keywords LIKE '%{$getters['searchInput']}%' || aut.authorname LIKE '%{$getters['searchInput']}%')");
break;
case 'srch_publisher':
array_push($queries, "(bk.publisher = '{$getters["srch_publisher"]}')");
break;
case 'Year':
if(isset($getters['Year1']) ==""){
array_push($queries, "(bk.year = '{$getters['Year']}')");
} else {
array_push($queries, "(bk.year BETWEEN '$value' AND '{$getters['Year1']}')");
}
break;
case 'srch_author':
if(isset($getters['authorOperator']) ==""){
array_push($queries, "(bk_aut.author_id = '{$getters["srch_author"]}')");
} else {
$operator = $getters['authorOperator'];
array_push($queries, "(bk_aut.author_id = '$value' $operator bk_aut.author_id = '{$getters['srch_author1']}')");
}
break;
case 'srch_category':
if(isset($getters['catOperator']) ==""){
array_push($queries, "(bk_cat.category_id = '{$getters["srch_category"]}')");
} else {
$operator1 = $getters['catOperator'];
array_push($queries, "(bk_cat.category_id = '$value' $operator1 bk_cat.category_id = '{$getters['srch_category1']}')");
}
break;
}
}
}
if(!empty($queries)){
$sql .= " WHERE ";
$i = 1;
foreach ($queries as $query) {
if($i < count($queries)){
$sql .= $query." AND ";
} else {
$sql .= $query;
}
$i++;
}
}
$sql .= " GROUP BY bk.title ORDER BY bk.title ASC";
var_dump($sql);
}else{
$sql .= " GROUP BY bk.title ORDER BY bk.title ASC";
}
$rs = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$tot_rows = mysql_num_rows($rs);
?>
这是发送到数据库的SQL Dump,
SELECT
DISTINCT bk.title AS Title,
bk.bookid AS BookID,
bk.year AS Year,
bk.publisher AS Publisher,
aut.authorname AS Author
FROM book bk
JOIN book_category bk_cat ON bk_cat.book_id = bk.bookid
JOIN categories cat ON cat.id = bk_cat.category_id
JOIN books_authors bk_aut ON bk_aut.book_id = bk.bookid
JOIN authors aut ON aut.id = bk_aut.author_id
WHERE
(bk_cat.category_id = '2' AND bk_cat.category_id = '1')
GROUP BY bk.title
ORDER BY bk.title ASC
答案 0 :(得分:2)
格式化查询以使其实际可读后,您会看到以下行:
bk_cat.category_id = '2' AND bk_cat.category_id = '1'
这不起作用,category_id
不能同时为2
和1
。
您的查询应如下所示:
SELECT books.*
FROM books
JOIN book_categories bc1 ON books.id = bc1.book_id AND bc1.category_id = 1
JOIN book_categories bc2 ON books.id = bc2.book_id AND bc1.category_id = 2
您需要在类别上JOIN
两次,或者您需要匹配类别的次数。
答案 1 :(得分:2)
有多种方法可以做到这一点,但最简单的方法是使用WHERE
这样的SELECT
条款到WHERE
`bk`.`book_id` IN (SELECT `book_id` FROM `book_category` WHERE `category_id` = '2')
AND `bk`.`book_id` IN (SELECT `book_id` FROM `book_category` WHERE `category_id` = '1')
本书,这些条款属于多个类别:
{{1}}