为此,请在MySQL中使用BETWEEN运算符。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Start int, -> End int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Start,End) values(100,200); mysql> insert into DemoTable(Start,End) values(400,500); mysql> insert into DemoTable(Start,End) values(210,350);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-------+------+ | Id | Start | End | +----+-------+------+ | 1 | 100 | 200 | | 2 | 400 | 500 | | 3 | 210 | 350 | +----+-------+------+ 3 rows in set (0.00 sec)
以下是查询以查找MySQL中范围之间的值-
mysql> select *from DemoTable WHERE 432 BETWEEN Start AND End;
这将产生以下输出-
+----+-------+------+ | Id | Start | End | +----+-------+------+ | 2 | 400 | 500 | +----+-------+------+ 1 row in set (0.04 sec)