搜索具有多个条件的内部联接

时间:2014-10-03 11:15:33

标签: php mysql search join inner-join

我正在尝试创建一个搜索功能,搜索书名,书籍作者,书籍ISBN或通过关键字(通过内部联接复合表连接到书籍)。我没有得到我想要的结果;

取决于我搜索到的结果:

  • 如果搜索字词例如出现在图书标题和关键字名称列中,它有时会返回重复的结果。如果我正在寻找" hulk"和" hulk"出现在书的标题上,并且该书加入了关键字,它将同一本书返回两次等。
  • 它只搜索关键字表并忽略另一个" OR WHERE"条款,以便在其他指定的列中搜索。

我已经尝试过" SELECT DISTINCT .." sql语句,但仍然无法正常工作

数据库中的表:

  1. 书桌//书籍信息是商店

    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 ;
    
  2. 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;
    
  3. 关键字表

    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 ;
    
  4. /// 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
    
            }
    

2 个答案:

答案 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

抱歉,我无法对此进行测试,因为我没有可以使用的数据库。