简体   繁体   English

改进我的sql查询,以便在表的所有列中搜索文本

[英]Improving the my sql query for doing search for text in all columns of the table

I have to implement the feature where in user inputs some text in the search field and I need to query and pull out the items related to that text from the table.In the table I have close to 8 columns which are user facing and 4 columns which are auto inserted whenever insert happens via front end. 我必须实现该功能,即用户在搜索字段中输入一些文本,并且需要从表中查询并拉出与该文本相关的项目。在表中,我有近8列面向用户和4列只要通过前端插入就会自动插入。

Below is query I wrote and it works in via mysql client, 下面是我编写的查询,它可以通过mysql客户端运行,

select SQL_CALC_FOUND_ROWS * 
from `database`.`ttable` 
WHERE testdatetime BETWEEN '2012/05/01' and '2012/09/27' 
and testimg IS NOT NULL 
and testtitle LIKE 't%' 
OR testname LIKE 't%' 
OR testaddr LIKE 't%'  
OR testph LIKE 't%' 
OR testoffs LIKE't%' 
OR testmaps LIKE 't%' 
OR testimg LIKE 't%' 
OR testdatetime LIKE 't%'  
order  by testdatetime desc;

Now for some reason this query looks bit odd to me as I am checking each column for that value, and wanted to check if this is right way to implement it or mysql has some built it features which yields better results than this query.Appreciate if someone can point me to right direction.Since I am yet to implement this feature,I dont know how it performs in terms of performance,but I know Like clauses are bit intensive in operations. 现在由于某种原因,这个查询对我来说有点奇怪,因为我正在检查每个列的值,并想检查这是否是实现它的正确方法,或者mysql具有一些内置的功能,它产生的结果比此查询更好。有人可以为我指出正确的方向。由于我尚未实现此功能,因此我不知道它在性能方面的表现,但我知道Like子句在操作中会占用很多时间。

You might be better off using full text search for your string searches rather than like statements. 使用全文搜索进行字符串搜索可能比使用类似语句更好。 That way you can also find variants of the search terms. 这样,您还可以找到搜索字词的变体。 Here's a link on full-text searches for MySQL http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html 这是MySQL全文搜索的链接http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

MySQL has a built in feature to do what you want. MySQL具有内置功能来执行您想要的操作。 It's called a full text index. 它称为全文索引。 It will be way more efficient than a bunch of like statements. 它比一堆类似的语句更有效。 Here's something to get you started. 这是一些可以帮助您入门的东西。

http://www.databasejournal.com/sqletc/article.php/1578331/Using-Fulltext-Indexes-in-MySQL---Part-1.htm http://www.databasejournal.com/sqletc/article.php/1578331/Using-Fulltext-Indexes-in-MySQL---Part-1.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM