在 MySQL 中获取某个范围内值的 BETWEEN 子句有哪些替代方案?
为了避免使用 BETWEEN 子句,您可以使用 AND 来获取范围内值。我们首先创建一个表 -
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.62 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(85); Query OK, 1 row affected (0.12 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable −
这将产生以下输出 -
+--------+ | Number | +--------+ | 10 | | 20 | | 60 | | 100 | | 90 | | 85 | +--------+ 6 rows in set (0.00 sec)
以下是未使用 BETWEEN 子句在范围内获取值的查询 -
mysql> select *from DemoTable where 15 <=Number and Number <= 80;
这将产生以下输出 -
+--------+ | Number | +--------+ | 20 | | 60 | +--------+ 2 rows in set (0.00 sec)
广告