在这里您可以使用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)