4

I have a table the contains a VARCHAR column called - DOC_NAME. I would like to query this column for any string matches, partial matches are accepted as well.

For example, the column contains:

"How dangerous Virus and Spyware attach on the system – Techicode"

If I search for "virus spyware" I get no results. I don't want to search each word separately, as this might return irrelevant results. Also, I want to search for 'keywords' and not necessary the whole string.

I've tried the following options:

select * from docs where DOC_NAME like '%virus spyware%';

I've also tried:

SELECT * FROM docs WHERE DOC_NAME LIKE CONCAT('%','virus spyware','%')

But as I've mentioned, I don't any results back.

I there a query I can use that will return a value?

Thanks in advance.

2
  • It should be '%virus and spyware%' Commented Nov 11, 2014 at 21:44
  • @gpullen That's exactly what I'm trying to avoid, I want to search just 'keywords' not the whole string. Commented Nov 11, 2014 at 21:46

4 Answers 4

13
select * from docs where DOC_NAME like '%virus%spyware%'

or if the order doesn't matter and only one needs to match:

select * from docs where DOC_NAME like '%virus%' or DOC_NAME like '%spyware%'

if all keywords must match use AND instead of OR.

Sign up to request clarification or add additional context in comments.

6 Comments

@Tim_Schmelter Is there a way to use a dynamic 'select' query? As I do not know how many words I would require to match.
You can use dynamic sql and you can generate dynamic sql/parameters(but you haven't even mentioned where you use/generate the sql). Or you could fill a (temp-)table with the key-words and use that to join with this table.
Can you please provide with an example query for this issue? Using dynamic SQL? As I would require this wildcard and dynamic search for multiple tables
@DavidFaiz: how and from where do you pass the parameters at all? You know how to insert records into a temp-table? Then you could use Join or (better) Exists: select * from docs d where exists(select 1 from keywords kw where d.DOC_NAME like kw.Word).
I'm quite new to SQL and especially dynamic SQL. The App's logic is running on Java, so the idea is the input string will be divided into words and we will query the database like you suggested. But given that the string length is unknown, I will have to learn dynamic SQL
|
4

There are multiple options to implement fulltext search in MySQL. The native way is to add a fulltext index, which can then be used to run searches like your example:

alter table docs
  add fulltext index doc_name_fti (doc_name);

select * 
from docs
where match(doc_name) against ('+virus +spyware' in boolean mode);

There are also products like Sphinx and Solr you can use.

Comments

0

SELECT doc_name FROM docs WHERE doc_name RLIKE '.*virus*spyware'

I find the LIKE syntax less familiar than PCRE.

However, this is probably more performant

SELECT FIELD('virus', doc_name) or FIELD('spyware', doc_name) from docs

Comments

0

Fulltext indexing is the best way to achieve this. If for whatever reason you can't and must use LIKE, then for an ALL terms search:

select * from docs where DOC_NAME like '%virus%' and DOC_NAME like '%spyware%';

For an ANY terms search:

select * from docs where DOC_NAME like '%virus%' or DOC_NAME like '%spyware%';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.