SQL之间,MySQL在日期之间,而不是之间

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.

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

RollNoStudentNameStudentGenderStudentAgeStudentPercentAdmissionDate
1GeorgeM14852018-01-01
2MonicaF12882018-01-31
3JessicaF13842018-01-15
4TomM11782017-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:

输出:

RollNoStudentNameStudentAge
1George14
3Jessica13
卷号 学生姓名 学生年龄
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:

输出:

RollNoStudentNameStudentAge
2Monica12
4Tom11
卷号 学生姓名 学生年龄
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:

SQL Multiple Between Operators

输出:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值