Search code examples
mysqldesign-patternswildcardsql-like

How to write a query when pattern is in field


I've got a table in which a field contains pattern like 'hey *' or '%test%'. Like this :

Id - f_pattern - f_Respond
1  - 'hey *' - 'hello there'
2  - 'how are you *' - 'am fine'

Is is possible that i write query like this :

select * from table where f_pattern like 'hey bobby'

and it returns the first row ?


Solution

  • Yes, if you change your patterns to "like compatible" values, and reverse the like logic:

    select * from pattern_table
    where 'hey bobby' like replace(f_pattern, '*', '%')