SQL basic knowledge V2-BETWEEN

SQL column

Summary of basic knowledge of SQL database

Summary of advanced knowledge of SQL database

The role of BETWEEN

The BETWEEN operator is used to select a value in the data range between two values.

The boundary
BETWEEN operator of BETWEEN selects values ​​within a given range. The value can be a number, text or date.

The BETWEEN operator is inclusive: including start and end values, equivalent to >= AND <=

The syntax of BETWEEN

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

The
following is the data in the "Products" table of the sample database :

SQL basic knowledge V2-BETWEEN

BETWEEN Example The
following SQL statement selects all products with a price between 30 and 60:

Instance

SELECT * FROM Products
WHERE 价格 BETWEEN 30 AND 60;

result:

SQL basic knowledge V2-BETWEEN

You can see that the data in the price column is between 30 (inclusive) and 60 (inclusive)

NOT BETWEEN example
To display products outside the range of the previous example, use NOT BETWEEN:

Instance


SELECT * FROM Products
WHERE 价格 NOT BETWEEN 30 AND 60;
SELECT * FROM Products
WHERE NOT  价格 BETWEEN 30 AND 60;

The NOT position here can be before or after the column, and the result is the same, just personal writing habits

result:

SQL basic knowledge V2-BETWEEN

The price column will show that numbers between 30 and 60 have been excluded.

Example of the BETWEEN operator with IN The
following SQL statement selects all products whose prices are between 10 and 60 but whose names are not rice and bananas:

Instance


SELECT * FROM Products
WHERE (价格 BETWEEN 10 AND 60)
AND 名称 NOT  IN ('大米','香蕉');

Because both rice and banana are character types, single quotes ('') are used

result:

SQL basic knowledge V2-BETWEEN

It can be seen that the price column is between 10 and 60, and the price of the banana (45.00) column in the name column is also consistent, but because we excluded him with NOT IN, it is not displayed.

Example of the BETWEEN operator with a text value The
following SQL statement selects all products with the names BETWEEN'bread' and'banana':

Instance


SELECT * FROM Products
WHERE 名称 BETWEEN '面包' AND '香蕉'

result:

SQL basic knowledge V2-BETWEEN

Did you notice?

Why did Apple appear?

This is because the database is sorted according to the ASCII of the first letter of the pinyin of the name when the database is filtered. The initial letter of bread is M, the initial letter of banana is X, and the initial letter of apple is P just between them, so it is Included.

Then why is there no Sydney? The first letter of Sydney is also X?

When the first letter is the same, the database will continue to compare the second letter. If the second letter is also the same, the database will be compared sequentially until the comparison is complete. The full spelling of bananas here is XIANGJIAO, while the full spelling of Sydney is XUELI. Obviously, the second letter U in ascending order is larger than the letter I, which has exceeded the upper limit of the letter I, so Sydney was excluded.

Example of NOT BETWEEN operator with text value The
following SQL statement selects all products whose names are not BETWEEN'bread' and'banana':

Instance


SELECT * FROM Products
WHERE 名称 NOT BETWEEN '面包' AND '香蕉'

result

SQL basic knowledge V2-BETWEEN

Excluding the number between bread and bananas, rice and Sydney are left.

The
sample table Orders for date boundary problems
The following is data selected from the "Orders" table:

SQL basic knowledge V2-BETWEEN

Example of the BETWEEN operator with a date value The
following SQL statement selects all orders whose order dates are between '2018-06-28' and '2018-09-28':

Instance


SELECT * FROM Orders
WHERE  订单日期 BETWEEN '2018-06-28' AND '2018-09-28';

result:

Please note that BETWEEN AND may have unexpected results on the processing date

Between and in SQL includes the boundary value, not between does not include the boundary value, but if you use between and to limit the date, you need to pay attention, if the date after and is in the day, the default is 00:00:00 For example: after and The date is September 28, 2018, which is equivalent to 2018-09-28 00:00:00, so the data of 2018-09-28 11:24:54.000 cannot be found. If you want to find 2018-09- 28 this whole day of data, then you need to add 1 day when fetching the value, that is, BETWEEN '2018-06-28' AND '2018-09-29', so the return is June 28 (inclusive) to 9 All the data on the 28th of the month (inclusive).

Guess you like

Origin blog.51cto.com/15057820/2656349