📅  最后修改于: 2020-11-27 06:21:04             🧑  作者: Mango
您已经看到MySQLi模式与LIKE …%匹配。 MySQLi支持基于正则表达式和REGEXP运算符的另一种模式匹配操作。如果您了解PHP或PERL,那么对您来说很容易理解,因为这种匹配与那些脚本正则表达式非常相似。
以下是模式表,可以与REGEXP运算符一起使用。
Pattern | What the pattern matches |
---|---|
^ | Beginning of string |
$ | End of string |
. | Any single character |
[…] | Any character listed between the square brackets |
[^…] | Any character not listed between the square brackets |
p1|p2|p3 | Alternation; matches any of the patterns p1, p2, or p3 |
* | Zero or more instances of preceding element |
+ | One or more instances of preceding element |
{n} | n instances of preceding element |
{m,n} | m through n instances of preceding element |
现在,基于上表,您可以配置各种类型的SQL查询以满足您的要求。在这里,我列出的只是一些供您理解。考虑一下我们有一个名为tutorials_inf的表,它有一个名为name的字段-
查询以找到所有以“ sa”开头的名称
mysql> SELECT * FROM tutorials_inf WHERE name REGEXP '^sa';
样本输出应该像这样-
+----+------+
| id | name |
+----+------+
| 1 | sai |
+----+------+
1 row in set (0.00 sec)
查询以找到所有以“ ai”结尾的名称
mysql> SELECT * FROM tutorials_inf WHERE name REGEXP 'ai$';
样本输出应该像这样-
+----+------+
| id | name |
+----+------+
| 1 | sai |
+----+------+
1 row in set (0.00 sec)
查询以查找所有包含“ a”的名称
mysql> SELECT * FROM tutorials_inf WHERE name REGEXP 'a';
样本输出应该像这样-
+----+-------+
| id | name |
+----+-------+
| 1 | sai |
| 3 | ram |
| 4 | johar |
+----+-------+
3 rows in set (0.00 sec)
查询以元音开头的所有名称
mysql> SELECT * FROM tutorials_inf WHERE name REGEXP '^[aeiou]';