The SQL BETWEEN operator is used along with WHERE clause for providing a range of values. The values can be the numeric value, text value, and date.

SQL BETWEEN运算符与WHERE子句一起使用以提供一系列值。 这些值可以是数字值,文本值和日期。

SQL BETWEEN operator is almost like SQL IN operators used in a sequential manner.

SQL BETWEEN运算符几乎与按顺序使用的SQL IN运算符相似。

The values are defined as part of the BETWEEN range are inclusive i.e. the values that are mentioned in the range are included at the start and end values.

这些值被定义为BETWEEN范围的一部分(包括端值),即该范围中提到的值包括在开始值和结束值中。

Let’s discuss in detail about the BETWEEN operator.

让我们详细讨论BETWEEN运算符。

As mentioned above BETWEEN operator can be used along with numeric value, text value, and date. We will discuss all the three in detail below.

如上所述,BETWEEN运算符可以与数字值,文本值和日期一起使用。 我们将在下面详细讨论所有三个。

语法之间SQL (SQL Between Syntax)

SELECT Column(s) FROM table_name WHERE column BETWEEN value1 AND value2;

Using the above-mentioned syntax, we can define values as part of BETWEEN operator. Also, the syntax mentioned above remains the same for usage with a numeric value, text value, and date value.

使用上述语法,我们可以将值定义为BETWEEN运算符的一部分。 同样,上述语法在使用数字值,文本值和日期值时也保持不变。

SQL BETWEEN运算符的数值 (SQL BETWEEN operator for Numeric value)

We will understand the above-mentioned syntax in more detail through some examples for numeric value.

我们将通过一些数值示例来更详细地了解上述语法。

Let’s consider the following Student table for example purpose.

让我们考虑以下学生表格作为示例。

RollNo StudentName StudentGender StudentAge StudentPercent AdmissionDate
1 George M 14 85 2018-01-01
2 Monica F 12 88 2018-01-31
3 Jessica F 13 84 2018-01-15
4 Tom M 11 78 2017-12-15
卷号 学生姓名 学生性别 学生年龄 学生百分比 入学日期
1个 乔治 中号 14 85 2018-01-01
2 莫妮卡 F 12 88 2018-01-31
3 杰西卡(Jessica) F 13 84 2018-01-15
4 汤姆 中号 11 78 2017-12-15

I am using MySQL database and here is the script to create and insert example records in the Student table.

我正在使用MySQL数据库,这是用于在Student表中创建和插入示例记录的脚本。

CREATE TABLE `Student` (`rollno` int(11) unsigned NOT NULL,`studentname` varchar(20) DEFAULT NULL,`studentgender` varchar(5) DEFAULT NULL,`studentage` int(3) DEFAULT NULL,`studentpercent` int(3) DEFAULT NULL,`admissiondate` date DEFAULT NULL,PRIMARY KEY (`rollno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `Student` (`rollno`, `studentname`, `studentgender`, `studentage`, `studentpercent`, `admissiondate`)
VALUES(1, 'George', 'M', 14, 85, '2018-01-01'),(2, 'Monica', 'F', 12, 88, '2018-01-31'),(3, 'Jessica', 'F', 13, 84, '2018-01-15'),(4, 'Tom', 'M', 11, 78, '2017-12-15');

Scenario: Get the percentage of students whose age is between 11 and 13.

方案 :获取11至13岁之间的学生所占的百分比

SELECT StudentPercent FROM Student WHERE StudentAge BETWEEN 11 AND 13;

Output:

输出:

StudentPercent
88
84
78
学生百分比
88
84
78

SQL NOT BETWEEN数值运算符 (SQL NOT BETWEEN Operator for Numeric Value)

The SQL NOT BETWEEN operator is used for getting the values as part of result set which is outside of the range specified by the BETWEEN operator.

SQL NOT BETWEEN运算符用于将值作为结果集的一部分获取,该值超出BETWEEN运算符指定的范围。

Scenario: Get the percentage of students whose age is not between 11 and 13.

方案 :获取年龄在11到13岁之间的学生所占的百分比

SELECT StudentPercent FROM Student WHERE StudentAge NOT BETWEEN 11 AND 13;

Output:

输出:

StudentPercent
85
学生百分比
85

SQL BETWEEN文本值运算符 (SQL BETWEEN operator for Text value)

Scenario: Get the RollNo, StudentName and StudentAge where StudentName is between George and Jessica.

场景 :获取RollNo,StudentName和StudentAge,其中StudentName在George和Jessica之间。

SELECT RollNo, StudentName, StudentAge FROM Student WHERE StudentName BETWEEN 'George' AND 'Jessica';

Output:

输出:

RollNo StudentName StudentAge
1 George 14
3 Jessica 13
卷号 学生姓名 学生年龄
1个 乔治 14
3 杰西卡(Jessica) 13

SQL NOT BETWEEN文本值运算符 (SQL NOT BETWEEN Operator for Text Value)

Scenario: Get the RollNo, StudentName and StudentAge where StudentName is not between George and Jessica.

场景 :获取RollNo,StudentName和StudentAge,其中StudentName不在George和Jessica之间。

SELECT RollNo, StudentName, StudentAge FROM Student WHERE StudentName NOT BETWEEN 'George' AND 'Jessica';

Output:

输出:

RollNo StudentName StudentAge
2 Monica 12
4 Tom 11
卷号 学生姓名 学生年龄
2 莫妮卡 12
4 汤姆 11

SQL BETWEEN运算符的日期值 (SQL BETWEEN operator for Date value)

Scenario: Get the age of students whose admission is between 1st Jan 2018 and 31st Jan 2018.

方案 :获取2018年1月1日至2018年1月31日之间入学的学生的年龄。

SELECT StudentAge FROM Student WHERE admissiondate BETWEEN str_to_date('2018-01-01', '%Y-%m-%d') AND '2018-01-31';

Output:

输出:

StudentAge
14
12
13
学生年龄
14
12
13

Note that I am using MySQL native function str_to_date to convert string to date. If the string is in default format, we can use it as-is too, just as I have used for the second argument.

请注意,我正在使用MySQL本机函数str_to_date将字符串转换为日期。 如果字符串是默认格式,我们也可以按原样使用它,就像我在第二个参数中使用的一样。

If you are using Oracle DB, then corresponding function is TO_DATE.

如果您使用的是Oracle DB,则对应的函数为TO_DATE

SQL NOT BETWEEN日期值运算符 (SQL NOT BETWEEN Operator for Date Value)

Scenario: Get the age of students whose admission is not between 1st Jan 2018 and 31st Jan 2018.

方案 :获取不在2018年1月1日至2018年1月31日之间入学的学生的年龄。

SELECT StudentAge FROM Student WHERE admissiondate NOT BETWEEN str_to_date('2018-01-01', '%Y-%m-%d') AND '2018-01-31';

Output:

输出:

StudentAge
11
学生年龄
11

运算符之间有多个 (MULTIPLE BETWEEN operators)

We can use multiple between operators too. Its syntax is:

我们也可以在运算符之间使用多个。 其语法为:

SELECT Column(s) FROM table_name WHERE
column_name BETWEEN value1 AND value2
AND
column_name BETWEEN value3 and value4
...
AND
BETWEEN column_name BETWEEN valueN and valueM;

Using the above-mentioned syntax, we can use multiple BETWEEN operators.

使用上述语法,我们可以使用多个BETWEEN运算符。

Scenario: Get the student name with age between 10 and 13 and marks between 80 to 85 percentage.

场景 :获取年龄在10到13岁之间且分数在80到85之间的学生姓名。

SELECT StudentName FROM Student WHERE
StudentAge BETWEEN 10 AND 13
AND
StudentPercent BETWEEN 80 AND 85;

Output:

输出:

翻译自: https://www.journaldev.com/23954/sql-between-mysql-between-dates-not

SQL之间,MySQL在日期之间,而不是之间相关推荐

  1. Sql确定两个日期之间的工作日数目

    问题 给定两个日期,求它们之间(包括这两个日期本身)有多少个"工作"日.例如,如果1月10日是星期一,1月11日是星期二,由于这两个日期是典型的工作日,所以两个日期之间的工作日数是 ...

  2. SQL Server中唯一索引和唯一约束之间的区别

    This article gives you an overview of Unique Constraints in SQL and also the Unique SQL Server index ...

  3. 日期与unix时间戳之间的转换C++实现

    之前在https://blog.csdn.net/fengbingchun/article/details/107023645 中介绍过gmtime和localtime的区别,这里介绍下日期与Unix ...

  4. Python和JavaScript之间的JSON日期时间

    本文翻译自:JSON datetime between Python and JavaScript I want to send a datetime.datetime object in seria ...

  5. SQL Server - 使用 Merge 语句实现表数据之间的对比同步

    SQL Server - 使用 Merge 语句实现表数据之间的对比同步 原文:SQL Server - 使用 Merge 语句实现表数据之间的对比同步 表数据之间的同步有很多种实现方式,比如删除然后 ...

  6. java两个日期之间所有日期_java如何输出指定两个日期之间的所有日期

    java如何输出指定两个日期之间的所有日期 关注:252  答案:3  mip版 解决时间 2021-01-31 04:38 提问者等妳¬硪唯一鍀执念 2021-01-31 01:40 java如何输 ...

  7. 计算SharePoint两个日期和时间字段之间的时间差值

    计算SharePoint两个日期和时间字段之间的时间差值 在SharePoint中,有一个"日期和时间(Date and Time)"类型的字段,使用此字段不仅可以只存储日期值,还 ...

  8. PHP两个日期之间的所有日期

    我想得到两个日期之间的所有日期,  例如: 输入两个日期,把这两个日期之间的所有日期取出来       如果是:2005-02-01至2005-02-05(同为一个月)       则为:2005-0 ...

  9. 2012.2 今天是2012年4月12日星期四,编写程序,输入今天开始到12月31日之间的任意日期

    //今天是2012年4月12日星期四,编写程序,输入今天开始到12月31日之间的任意日期,//输出那一天是星期几.例如 输入:5 20 (5月20),输出:sunday//monday tuesday ...

最新文章

  1. 独家 | 增强数据库管理:一份简单的综述
  2. 2.innodb后台线程
  3. IPv6 — 协议头
  4. 服务器预装操作系统,服务器预装操作系统吗
  5. LeetCode11:Container With Most Water
  6. 计算机键盘音乐好汉歌,好汉歌 MIDI File Download :: MidiShow
  7. Window.document对象
  8. 安卓前端布局Android,Android开发的几种常见布局
  9. enter 默认搜索
  10. VS2010解决闪退的方法
  11. 【渝粤题库】国家开放大学2021春2737市场调查与商情预测题目
  12. 剑指 Offer 09. 用两个栈实现队列(day 03)
  13. zabbix自定义监控之声音报警、用户和组权限
  14. 人工智能机器学习Java也可以
  15. ffmpeg翻译文档
  16. Lab2 Defusing a Binary Bomb
  17. android iCloud 短信,云助手 基于安卓的iCloud
  18. redis的failover ,redmon安装
  19. CTF——MISC习题讲解(GKCTF 2021系列)
  20. 洛谷P1336 课题选择

热门文章

  1. 20140708testC
  2. FreeBSD下nginx并支持php配置详解
  3. [转载] python mongodb update
  4. [转载] python中list的方法有哪些_Python 列表(list)中的方法
  5. [转载] python 字符串查找的4个方法和count函数
  6. [转载] numpy逆 python_Python之Numpy详细教程,附Python最新学习资料
  7. [转载] python2.7中模块学习- textwrap 文本包装和填充
  8. [转载] 【数据处理】 python 极速极简画图——频数(率)分布直方图
  9. Weblogic的安装与卸载
  10. java 同步块(Java Synchronized Blocks)