Mysql 使用my matchstring搜索包含正则表达式的行
我的用户在应用程序中输入URL的正则表达式。我必须返回与给定url匹配的正则表达式行 我有一张这样的桌子:Mysql 使用my matchstring搜索包含正则表达式的行,mysql,regex,Mysql,Regex,我的用户在应用程序中输入URL的正则表达式。我必须返回与给定url匹配的正则表达式行 我有一张这样的桌子: +----+-----------------------+--------------------------+ | id | Survey | URL Regexp | +----+-----------------------+--------------------------+ | 1 | User Age survey
+----+-----------------------+--------------------------+
| id | Survey | URL Regexp |
+----+-----------------------+--------------------------+
| 1 | User Age survey | http://*.google.com/* |
| 2 | Payment intent survey | http://mail.google.com/* |
| 3 | User Country info | *reader.google.com* |
| 4 | Company information | http://facebook.com/* |
| 5 | Satisfaction survey | http://*twitter.com/* |
+----+-----------------------+--------------------------+
SELECT * FROM surveys where MATCH_REGEXP('http://mail.google.com/index.html')
我想对url运行查询,http://mail.google.com/index.html
并参见(1,2)
(即用户年龄调查
和支付意向调查
记录)
我希望运行如下查询:
+----+-----------------------+--------------------------+
| id | Survey | URL Regexp |
+----+-----------------------+--------------------------+
| 1 | User Age survey | http://*.google.com/* |
| 2 | Payment intent survey | http://mail.google.com/* |
| 3 | User Country info | *reader.google.com* |
| 4 | Company information | http://facebook.com/* |
| 5 | Satisfaction survey | http://*twitter.com/* |
+----+-----------------------+--------------------------+
SELECT * FROM surveys where MATCH_REGEXP('http://mail.google.com/index.html')
我找不到任何关于这种表达式的文档。解决这个问题的最佳方法是什么?下面是一个SQLFIDLE示例
鉴于您拥有这些数据:
CREATE TABLE surveys (
id int auto_increment primary key,
survey varchar(30),
url_regex varchar(30)
);
INSERT INTO surveys (survey, url_regex) VALUES
('User Age survey', 'http://.*.google.com/*'),
('Payment intent survey', 'http://mail.google.com/*'),
('User Country info', '*reader.google.com*'),
('Company information', 'http://facebook.com/*'),
('Satisfaction survey', 'http://*twitter.com/*');
您可以执行此查询以实现所需:
SELECT id FROM surveys
WHERE 'http://mail.google.com/index.html' REGEXP url_regex;
结果将是:
1
2
请注意,为了获得期望的结果,即
(1,2)
,“用户年龄调查”正则表达式必须通过在第一个星号前加一个点来固定 那么,问题又是什么?你的问题是什么?很抱歉含糊不清,为了清晰起见添加了更多细节。太棒了,谢谢!而且,我从来不知道sqlfiddle。谢谢你。