Luka Luka - 1 month ago 14
MySQL Question

MySql select rows via patterns

Take the following

patterns
table:

| id | pattern |
--------------------------------------------------------------------------
| 0 | date in {int} day|days|week|weeks|month|months|year|years |


As you can see, the mock-up pattern in row
0
expects the third word to be an integer and the fourth word to match one within the stated collection.

How would I go about selecting rows, given an inputted sentence, that matches some type of regular expression residing in the
pattern
field (similar to how I expressed above), eg.

select * from patterns where match(pattern, 'date in 25 weeks');

# result - row with id: 0


Thanks

Answer

You can rewrite your patterns in regular expressions and then build a query this way:

SELECT * FROM patterns WHERE 'date in 25 weeks' REGEXP pattern;