SQL Query reg.ex.匹配

时间:2011-10-03 16:39:49

标签: mysql sql regex

我有一个小问题,弄清楚哪个请求(MySQL)符合这个要求:

表1

+++++++++++++++++
id| name
+++++++++++++++++
1 | John Smith
2 | Eric Smith
3 | Martha Smith
4 | Smith
5 | Ronald Smith Donald
6 | Marc Fisher

表2

+++++++++++++++++++++++
regex | value1 | value2
+++++++++++++++++++++++
Smith | Mister | 356
Fisher| Sir    | 24

选择结果值为

结果设置

+++++++++++++++++++++++++++++++++++++++++++++++++++
id| name                | regex  | value1 | value2 
+++++++++++++++++++++++++++++++++++++++++++++++++++
1 | John Smith          | Mister | 356
2 | Eric Smith          | Mister | 356
3 | Martha Smith        | Mister | 356
4 | Smith               | Mister | 356
5 | Ronald Smith Donald | Mister | 356
6 | Marc Fisher         | Sir    | 24

提前致谢。

1 个答案:

答案 0 :(得分:2)

你可以使用MySQL的regexp

select  *
from    Table1 t1
join    Table2 t2
on      t1.name regexp t2.regex

如果名称与表2中的多个regex匹配,则会发生有趣的事情。