Combine text with wildcards in a database?

Given the input string, I would like to return the rows from a database (MySQL) containing a wildcard expression that will match the string. For example, if I have a table containing these wildcard expressions:

foo.*
foo.bar.*
erg.foo.*

and my input line is "foo.bar.baz", then I should get the lines "foo. *" and "foo.bar. *".

Any ideas on how this can be implemented? I hope this can be accomplished with a query, but I may need to select all the relevant rows and match in the application.

+3
source share
1 answer
SELECT  *
FROM    mytable
WHERE   'foo.bar.baz' RLIKE match

MySQL.

, , :

SELECT  *
FROM    mytable
WHERE   'foo.bar.baz' LIKE REPLACE(REPLACE(REPLACE(REPLACE('\\', '\\\\'), '.', '\\.'), '%', '\\%'), '*', '%') ESCAPE '\\'
+3

Source: https://habr.com/ru/post/1732994/


All Articles