我正在尝试创建一个搜索功能,搜索书名,书籍作者,书籍ISBN或通过关键字(通过内部联接复合表连接到书籍)。我没有得到我想要的结果;
取决于我搜索到的结果:
我已经尝试过" SELECT DISTINCT .." sql语句,但仍然无法正常工作
数据库中的表:
书桌//书籍信息是商店
CREATE TABLE IF NOT EXISTS books (
book_id` int(11) NOT NULL AUTO_INCREMENT,
book_title` varchar(100) NOT NULL,
book_author` varchar(100) NOT NULL,
book_ISBN` varchar(100) NOT NULL,
book_description` varchar(1000) NOT NULL,
book_price` double NOT NULL,
book_image` varchar(100) NOT NULL,
book_stock_quantity` int(11) NOT NULL,
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=61 ;
Book_keyword表//此表是为图书分配关键字的地方
CREATE TABLE IF NOT EXISTS books_keyword (
books_keyword_book_id` int(10) NOT NULL,
books_keyword_keyword_id` int(10) NOT NULL,
KEY `BOOK_FK` (`books_keyword_book_id`),
KEY `KEYWORD_FK` (`books_keyword_keyword_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
关键字表
CREATE TABLE keyword (
keyword_id` int(10) NOT NULL AUTO_INCREMENT,
keyword_name` varchar(50) NOT NULL,
PRIMARY KEY (`keyword_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
/// php code /////
$search = $_POST["Search"]; // get text from textbox and insert into variable
$mysqli = new mysqli('localhost', 'root','','bookstore'); //connect to database
$searchSQL = "SELECT books.*, books_keyword.books_keyword_keyword_id AS keyword_id, keyword.keyword_name FROM books JOIN books_keyword ON (books.book_id = books_keyword_book_id) JOIN keyword ON(books_keyword.books_keyword_keyword_id = keyword_id) WHERE keyword_name LIKE '%" . $search . "%' OR book_title LIKE '%" . $search . "%' OR book_ISBN LIKE '" . $search ."' OR book_author LIKE '%" . $search ."%'";
$Results = $mysqli->query($SearchSql); //run sql
while($SearchRow = $Results->fetch_assoc()) //print out each result
{
echo '<P>'.$SearchRow["book_title"].'</P>'; //echo book title in the search results
}
答案 0 :(得分:0)
它忽略额外WHERE条件的原因是所有连接都是INNER连接。如果where子句中的任何内容匹配而不是强制执行JOIN,则使用左连接将显示结果。使用GROUP BY:
可以解决重复结果的问题SELECT books.*, books_keyword.books_keyword_keyword_id AS keyword_id, keyword.keyword_name
FROM books
LEFT JOIN books_keyword ON (books.book_id = books_keyword_book_id)
LEFT JOIN keyword ON(books_keyword.books_keyword_keyword_id = keyword_id)
WHERE keyword_name LIKE '%" . $search . "%' OR book_title LIKE '%" . $search . "%' OR book_ISBN LIKE '" . $search ."' OR book_author LIKE '%" . $search ."%'
GROUP BY books.book_id;
答案 1 :(得分:0)
我会更好地格式化SQL。这样可以更容易地发现错误。
SELECT
books.*,
GROUP_CONCAT(DISTINCT books_keyword.books_keyword_keyword_id SEPARATOR ',') AS keyword_ids,
GROUP_CONCAT(DISTINCT keyword.keyword_name SEPARATOR ',') AD keyword_names
FROM
books
JOIN books_keyword ON (books.book_id = books_keyword.books_keyword_book_id)
JOIN keyword ON (books_keyword.books_keyword_keyword_id = keyword.keyword_id)
WHERE
keyword.keyword_name LIKE '%".$search."%' OR
books.book_title LIKE '%".$search."%' OR
books.book_ISBN LIKE '".$search."' OR
books.book_author LIKE '%".$search ."%'";
GROUP BY
books.book_id
诀窍是按书分组。请注意,您可能有多个关键字匹配。因此,我使用了分组功能GROUP_CONCAT
。
抱歉,我无法对此进行测试,因为我没有可以使用的数据库。