English 中文(简体)
MySQL查询和文本搜索
原标题:MySQL queries and text search

我有这个查询:

select name, body
  from news
 where body like %MyWord%;

我使用MySQL数据库引擎。当在正文中发现MyWord时,此查询将返回名称和正文。

我在这里的问题是,当我在正文中搜索两个单词时,比如MyWord1,MyWord2或更多!

如果你知道该查询是由函数调用的(我无法每次修改该查询),那么我该怎么做?

问题回答

您可以使用一些邪恶的SQL注入(类似的):)

我假设你将“我的词语”传递给包含查询的函数,你不能改变它。如果“我的词语”看起来像这样会发生什么:

MyWord1% 或者 body 包含 %MyWord2

恶劣,我知道,你已经被警告了 ;)

如果你想找到两个不同的词,你可以这样做:

select name, body
  from news
 where body like %MyWord1% and body like %MyWord2%;

然而,这很快就会变得非常不好,你可以研究其他一些选择:

  • using fulltext search, as suggested by Pablo Santa Cruz (probably the simplest to use)
  • parse your body field into a subtable via a trigger upon insert
  • search the data by populating a lucene/solr index and searching off of that (a bit more complicated to setup and maintain but the the performance is very impressive.




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签