SQL operators learn --BETWEEN

Description link

BETWEEN role

BETWEEN operator used to select a value within the range between the two data values.

BETWEEN border

BETWEEN operator to select a value within a given range. Values ​​can be numeric, text or date.

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

BETWEEN syntax

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

Sample Database

The following is the data "Products" table:

 

 

BETWEEN examples

The following SQL statement selects all product prices between 30 and 60:

Examples

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

result:

Data can be seen that the price column are between 30 (inclusive) and 60 (inclusive) between

 

NOT BETWEEN examples

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

Examples

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

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

The location can NOT here in front of or behind the column, the results are the same, just a personal habit of writing

result:

Prices exclude that column will show the number is between 30-60 of the.

 

IN BETWEEN operator with the Example

The following SQL statement selects names but not the price of rice and bananas all products between 10-60:

Examples

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

Because rice and bananas are all character types, so use single quotes ( '')

result:

We can see a price between 10-60 are met, the name of a banana (45.00) would have been in line with the price, but because we use NOT IN him ruled out, so I do not show up.

 

BETWEEN operator example with text values

The following SQL statement selects all products with names BETWEEN 'bread' and 'banana' of:

Examples

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

result:

Did you notice?

Why does Apple do?

This is because during the screening database is sorted by the first letter of the name of the alphabet ASCII, bread is the first letter of the first letter M, the banana is X, and P is the first letter of Apple's just between them, it was It included.

So why has not it in Sydney? Sydney is also the first letter X ah?

In the first letter of the same database will continue to compare the second letter, if the second also followed the same relatively down until all contrast finish. Here banana spelling is XIANGJIAO, the spelling is Xueli Sydney, obviously ascending order larger than the second U-letter I, which is beyond the scope of this limit letter I, so Sydney was excluded.

 

NOT BETWEEN operator example with text values

The following SQL statement selects all product name is not BETWEEN 'bread' and 'banana' of:

Examples

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

result:

 

 

Exclude a number between bread and rice and bananas to the rest of Sydney.

 

Date of the border issue

Example Table Orders

The following is a selected "Orders" data table:

 

 

BETWEEN operator with the date values ​​Example

The following SQL statement to select the order date falls on all orders between '2018-06-28' and '2018-09-28':

Examples

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

result:

Please note, BETWEEN AND your intentions may be less than the result of the processing date

 SQL is in between and inclusive of, not between not inclusive, but if you use between and defining the date should be noted that, if a later date and is to-day, then the default is 00:00:00, for example: and after dated September 28, 2018, is equivalent to 2018-09-28 00:00:00, then 2018-09-2811: 24: 54.000 data of finding out, if you want to be found 2018-09- the 28 day data, so when the need to increase the value one day, namely BETWEEN '2018-06-28' AND '2018-09-29', this return is June 28 (inclusive) to 9 May 28 (inclusive) of all the data.

Guess you like

Origin www.cnblogs.com/-wenli/p/11566924.html