SQL 两个值之间的标准
SQL Standard between two values
我想知道,SQL 标准或 MySQL 当我们使用 'between' 时有区别吗?
更准确地说,在 SQL 标准中,如果我这样做:
A between 1 and 10
是否等于:
1 <= A and A <= 10
-- or
1 < A ans 1 < 10
MySQL的结果是否相同。
expr BETWEEN min AND max
If expr is greater than or equal to min and expr is less than or equal
to max, BETWEEN returns 1, otherwise it returns 0. This is equivalent
to the expression (min <= expr AND expr <= max) if all the arguments
are of the same type. Otherwise type conversion takes place according
to the rules described in Section 12.2, “Type Conversion in Expression
Evaluation”, but applied to all the three arguments.
它明确指出 1 和 10 之间的 A 将等于 1 <= A 和 A <= 10
在MySQL
令 "employee_tbl" table 包含员工姓名,daily_typing_pages 列。
+------+------+------------+--------------------+
| id | name | work_date | daily_typing_pages |
+------+------+------------+--------------------+
| 1 | John | 2007-01-24 | 250 |
| 2 | Ram | 2007-05-27 | 220 |
| 3 | Jack | 2007-05-06 | 170 |
| 3 | Jack | 2007-04-06 | 100 |
| 4 | Jill | 2007-04-06 | 220 |
| 5 | Zara | 2007-06-06 | 300 |
| 5 | Zara | 2007-02-06 | 350 |
+------+------+------------+--------------------+
如果我们运行在这个table上进行以下查询:
SELECT * FROM employee_tbl
WHERE daily_typing_pages BETWEEN 170 AND 300;
查询结果如下:
+------+------+------------+--------------------+
| id | name | work_date | daily_typing_pages |
+------+------+------------+--------------------+
| 1 | John | 2007-01-24 | 250 |
| 2 | Ram | 2007-05-27 | 220 |
| 3 | Jack | 2007-05-06 | 170 |
| 4 | Jill | 2007-04-06 | 220 |
| 5 | Zara | 2007-06-06 | 300 |
+------+------+------------+--------------------+
这意味着作品之间包含在内。结果数据包括170和300。
在 MSSQL 中
让"Products"table包含产品数据包括产品名称、价格等
ProductID ProductName Price
1 Chais 18
2 Chang 19
3 Aniseed Syrup 10
5 Chef Anton's Gumbo Mix 21.35
4 Chef Anton's Cajun Seasoning 22
如果我们运行在这个table上进行以下查询:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
查询得到以下数据,包括在 between 子句中给出的价格。
ProductID ProductName Price
1 Chais 18
2 Chang 19
3 Aniseed Syrup 10
所以在 MSSQL 之间作为包容性工作就像 MySQL。
你可以看看这个:http://www.tutorialspoint.com/mysql/mysql-between-clause.htm
还有这个
我想知道,SQL 标准或 MySQL 当我们使用 'between' 时有区别吗? 更准确地说,在 SQL 标准中,如果我这样做:
A between 1 and 10
是否等于:
1 <= A and A <= 10
-- or
1 < A ans 1 < 10
MySQL的结果是否相同。
expr BETWEEN min AND max
If expr is greater than or equal to min and expr is less than or equal to max, BETWEEN returns 1, otherwise it returns 0. This is equivalent to the expression (min <= expr AND expr <= max) if all the arguments are of the same type. Otherwise type conversion takes place according to the rules described in Section 12.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments.
它明确指出 1 和 10 之间的 A 将等于 1 <= A 和 A <= 10
在MySQL
令 "employee_tbl" table 包含员工姓名,daily_typing_pages 列。
+------+------+------------+--------------------+
| id | name | work_date | daily_typing_pages |
+------+------+------------+--------------------+
| 1 | John | 2007-01-24 | 250 |
| 2 | Ram | 2007-05-27 | 220 |
| 3 | Jack | 2007-05-06 | 170 |
| 3 | Jack | 2007-04-06 | 100 |
| 4 | Jill | 2007-04-06 | 220 |
| 5 | Zara | 2007-06-06 | 300 |
| 5 | Zara | 2007-02-06 | 350 |
+------+------+------------+--------------------+
如果我们运行在这个table上进行以下查询:
SELECT * FROM employee_tbl
WHERE daily_typing_pages BETWEEN 170 AND 300;
查询结果如下:
+------+------+------------+--------------------+
| id | name | work_date | daily_typing_pages |
+------+------+------------+--------------------+
| 1 | John | 2007-01-24 | 250 |
| 2 | Ram | 2007-05-27 | 220 |
| 3 | Jack | 2007-05-06 | 170 |
| 4 | Jill | 2007-04-06 | 220 |
| 5 | Zara | 2007-06-06 | 300 |
+------+------+------------+--------------------+
这意味着作品之间包含在内。结果数据包括170和300。
在 MSSQL 中
让"Products"table包含产品数据包括产品名称、价格等
ProductID ProductName Price
1 Chais 18
2 Chang 19
3 Aniseed Syrup 10
5 Chef Anton's Gumbo Mix 21.35
4 Chef Anton's Cajun Seasoning 22
如果我们运行在这个table上进行以下查询:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
查询得到以下数据,包括在 between 子句中给出的价格。
ProductID ProductName Price
1 Chais 18
2 Chang 19
3 Aniseed Syrup 10
所以在 MSSQL 之间作为包容性工作就像 MySQL。
你可以看看这个:http://www.tutorialspoint.com/mysql/mysql-between-clause.htm
还有这个