我需要使用 SQL 匹配下面提到的模式。
- “* 谢谢你”
- “* 音乐 *”
- “我喜欢音乐”
- “我喜欢*音乐和*绘画”
输入字符串如下所示,
“HI THANK YOU” -> 应该匹配“* THANK YOU”
“我非常喜欢音乐” -> 应该匹配“* MUSIC *”
有没有办法使用标准 SQL 语法来做到这一点。
请帮忙。
谢谢你。
我需要使用 SQL 匹配下面提到的模式。
输入字符串如下所示,
“HI THANK YOU” -> 应该匹配“* THANK YOU”
“我非常喜欢音乐” -> 应该匹配“* MUSIC *”
有没有办法使用标准 SQL 语法来做到这一点。
请帮忙。
谢谢你。
Oracle MySQL Sample Queries:
Case insensitive:
select * from database where column like '%THANK YOU%';
Case sensitive:
select * from database where column LIKE BINARY '%THANK YOU%';
Case insensitive:
select * from database where column REGEXP 'THANK YOU';
Oracle Queries:
select * from database WHERE REGEXP_LIKE(column, 'THANK YOU', 'i')
You can use LIKE in Oracle, but case insensitivity setup depends on the version.
Case sensitive:
select * from database WHERE column LIKE 'THANK YOU';
There is a lot of discussion about making Oracle's like case insensitive. It's easier to use the reg_exp version for that and set the i
flag for case insensitivity.
Update The original question was actually opposite of my first answer, and it has really opened up my head about quite a few apps that I'm building for datamining. Actually had this idea last night but the original poster posted it before I did (Fiddle was down). You can make Regex match both sides of the REGEXP function in MySQL. So if you store a list of expressions into the database, then you can actually compare the list of expressions against a table of entries or a single string.
I've added an SQL Fiddle here: http://sqlfiddle.com/#!8/701a0/2
Note that in the original example, the "regex" values in the database started with just an asterisk. In Regex, the actual string would need to be .* for possibly any character.
I've added a few examples for matching things like websites, US Toll Free Numbers, and gmail accounts.
INSERT INTO `myRegex` VALUES (1,'.* THANK YOU$'),(2,'MUSIC'),(3,'I LIKE .* MUSIC'),(4,'I LIKE .* MUSIC AND .* PAINTING'),(5,'^[0-9]?[ .-]?(800|866)'),(6,'\\.com$'),(7,'\\.net$'),(8,'\\.(gmail|googlemail)\\.'),(9,'THANK YOU');
Output
select * from myRegex where 'HI THANK YOU' REGEXP reg_expr;
select * from myRegex where 'THANK YOU MUCH' REGEXP reg_expr;
我找到了一种使用 MySQL 的方法,
SELECT * FROM Knowledge WHERE 'HI THANK YOU' REGEXP pattern_col;
pattern_col 包含正则表达式。