SQL研习录(11)——BETWEEN运算符
版权声明
- 本文原创作者:清风不渡
- 博客地址:https://blog.csdn.net/WXKKang
一、BETWEEN运算符
BETWEEN 运算符用于选取介于两个值之间的数据范围内的值,也就是说BETWEEN可以选择给定范围内的值,数字、文本或者日期都可以,因为它的选择是具有包含性的,所以我们只需指定它的开始值和结束值即可,首先我们来创建一组测试数据,代码如下:
CREATE TABLE student(
s_id varchar(50),
s_name varchar(50),
s_gender varchar(50)
)
INSERT INTO student (s_id,s_name,s_gender) VALUES ('S101','Lucy','female');
INSERT INTO student (s_id,s_name,s_gender) VALUES ('S102','Jack','male');
INSERT INTO student (s_id,s_name,s_gender) VALUES ('S103','Bruce','male');
INSERT INTO student (s_id,s_name,s_gender) VALUES ('S104','Tom','male');
INSERT INTO student (s_id,s_name,s_gender) VALUES ('S105','Jayce','male');
1、基本语法
BETWEEN运算符的基本语法如下:
SELECT column_name(s)
FROM table_name
WHERE column_name [NOT] BETWEEN value1 AND value2;
2、示例
例如,在上面的测试数据中,如果要获取s_id在‘S102’-‘S104’之间的学生资料,可使用如下代码:
SELECT * FROM student WHERE s_id BETWEEN 'S102' AND 'S104';
执行结果如下:
如果要获取s_id不在‘S102’-‘S104’之间的学生资料,可使用如下代码:
SELECT * FROM student WHERE s_id NOT BETWEEN 'S102' AND 'S104';
执行结果如下: