REGEX在MySQL中匹配整数6到10?
在这里您可以使用BETWEEN运算符。语法如下-
SELECT *FROM yourTableName WHERE yourColumnName BETWEEN 6 AND 10;
您可以像这样使用正则表达式。语法如下-
SELECT *FROM yourTableName WHERE yourColumnName REGEXP '10|[6-9]';
为了理解这两种语法,让我们创建一个表。创建表的查询如下-
mysql> create table RegularExpressionDemo -> ( -> Id int -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into RegularExpressionDemo values(1); mysql> insert into RegularExpressionDemo values(2); mysql> insert into RegularExpressionDemo values(3); mysql> insert into RegularExpressionDemo values(4); mysql> insert into RegularExpressionDemo values(5); mysql> insert into RegularExpressionDemo values(6); mysql> insert into RegularExpressionDemo values(7); mysql> insert into RegularExpressionDemo values(8); mysql> insert into RegularExpressionDemo values(9); mysql> insert into RegularExpressionDemo values(10);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from RegularExpressionDemo;
以下是输出-
+------+ | Id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | +------+ 10 rows in set (0.00 sec)
这是匹配整数6到10的查询。
情况1-使用BETWEEN运算符。查询如下-
mysql> select *from RegularExpressionDemo where Id between 6 and 10;
以下是输出-
+------+ | Id | +------+ | 6 | | 7 | | 8 | | 9 | | 10 | +------+ 5 rows in set (0.00 sec)
这是使用REGEXP来匹配6到10的整数的查询-
mysql> select *from RegularExpressionDemo where Id REGEXP '10|[6-9]';
以下是输出-
+------+ | Id | +------+ | 6 | | 7 | | 8 | | 9 | | 10 | +------+ 5 rows in set (0.01 sec)
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短