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 红包祝福语简短10字
18 周六早晨祝福语简短