MySQL搜索查询两列 [英] Mysql search query two columns

查看:78
本文介绍了MySQL搜索查询两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网站建立搜索功能,并希望按标题和作者两列进行搜索.

I'm bulding a search function for my web site and wish to search by two columns: title and author.

查询:

SELECT title, bookID, publisher, pubDate, book_image, books.authorID, authors.author FROM books, authors WHERE books.authorID = authors.authorID 
            AND title LIKE '$queryString%' OR author LIKE '$queryString%'LIMIT 5

当我按标题搜索时,这很好用,但是当我按作者搜索时,我得到了所有书籍的列表,其中所搜索的作者为作者.显然这是不正确的.

When I search by title this works fine, but when I search by author I get a list of all books with the searched for author as author. Obviously this is incorrect.

如何解决此查询以成功搜索标题或作者?

How can I fix this query to successfully search for either title or author?

推荐答案

尝试以下方法:

SELECT title, bookID, publisher, pubDate, book_image, books.authorID, authors.author 
FROM books left join  authors on  books.authorID = authors.authorID 
WHERE (title LIKE '$queryString%' OR author LIKE '$queryString%') LIMIT 5

我建议您在mysql中使用 全文 搜索.会更快,更有效

I would like to suggest you to use fulltext search in mysql.That will be more faster and efficient

这篇关于MySQL搜索查询两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆