Search code examples
mysqlsqlsearchfull-text-searchexact-match

Search a word and Get Predictive Search Words from String (not complete string) in Mysql


I have SQL Table Design like Listed Below

*-----------------------------------------
| id | title                              |
| -- | -----------------------------------|
| 1  | This is nice pen looking good      |
| 2  | This is nice pen looking elegent   |
| 3  | This is nice pen looking great     |
| 4  | This is nice pen looking best.     |
------------------------------------------

Example Query: Select * from table where title LIKE '%looking%'

when i try to search word "looking" using (like query) or using (Regular Expressions) like example query, I am getting complete string results mentioned below

Results

*------------------------------------
| title                              |
| -----------------------------------|
| This is nice pen looking good      |
| This is nice pen looking elegent   |
| This is nice pen looking great     |
| This is nice pen looking best.     |
-------------------------------------

What i want?

I want predective search words (not complete string)

How can i get below mentioned Results by searching word (looking) using SQL.

*-------------------
| title             |
| ------------------|
| looking good      |
| looking elegent   |
| looking great     |
| looking best.     |
--------------------

Please Suggest how can i write query for geting these types of results? Thanks


Solution

  • You can get the first next work using substring_index():

    Select t.*,
           substring_index(substring_index(concat(' ', title, ' '), ' looking ', -1), ' ', 1) as next-word
    from table
    where title LIKE '%looking%';
    

    Here is a db<>fiddle.