简体   繁体   中英

How to create an “Any Word” search?

What we're trying to do is create a text search for an 'Any Word' type of match. The table is setup for example like this:

ID | Keyword
-------------
1  | alpha, beta, charlie
2  | bravo, delta, echo
3  | alpha, delta, foxtrot

The query that we've attempted so far is like this:

SELECT keyword FROM KeywordTest
       WHERE keyword 
 IN (select Value FRom split('alpha, beta, charlie',','))

What we thought would happen is that we would get two results back, one for ID 1, and one for ID 3. But that's not what happens we're only getting ID 1.

What am I doing wrong, and how could I tweak this query to bring back a match for 'Any Words' which are passed in as parameters.

Thanks!

The problem is in the where, because keyward in not IN the select, the keyword is LIKE the select. You can try with a INNER JOIN:

SELECT  keyword 
FROM    KeywordTest a inner join
        split('alpha, beta, charlie',',') b on a.keyword like '%'+Value+'%'

I actually just tackled a very similar problem.

I believe what you want to take a look into is the LIKE keyword for SQL.

SELECT keyword FROM KeywordTest
       WHERE keyword LIKE %'alpha'%

Note, you may have to do some kind of split by delimiter to get your search results all into the LIKE query. (I can help with that if you need as well)

Also, keep in mind that the % is a wild card. Putting the % before and after the search criteria, will find that string within any context.

If we had put WHERE Keyword LIKE 'alpha'%, we would only find results which begin with alpha.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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