Home > Database > Mysql Tutorial > body text

MySQL BETWEEN Usage

黄舟
Release: 2016-12-27 17:28:05
Original
3901 people have browsed it

MySQL BETWEEN Syntax

The BETWEEN operator is used in a WHERE expression to select a range of data between two values. BETWEEN is used together with AND, the syntax is as follows:

WHERE column BETWEEN value1 AND value2
WHERE column NOT BETWEEN value1 AND value2
Copy after login

Usually value1 should be less than value2. When BETWEEN is preceded by the NOT operator, it means the opposite of BETWEEN, that is, selecting a value outside this range.

BETWEEN Example

Select user data with uid between 2 and 5:

SELECT * FROM user WHERE uid BETWEEN 2 AND 5
Copy after login

Return query results as follows:

MySQL BETWEEN Usage

In addition to numeric types, BETWEEN also supports string ranges. The following selects all users whose username is between a and j (and includes the single letter k/K):

SELECT * FROM user WHERE username BETWEEN 'a' AND 'k'
Copy after login

Character ranges are also supported Chinese characters, but generally speaking they don’t make much sense.

MySQL BETWEEN Boundary

Although almost all databases support the BETWEEN...AND operator, different databases handle BETWEEN...AND differently. In MySQL, BETWEEN contains value1 and value2 boundary values, such as the above example of selecting user data with uid between 2 and 5.

Some databases do not contain value1 and value2 boundary values ​​(similar to > and <), and some databases contain value1 but not value2 (similar to >= and <). So when using BETWEEN ... AND, please check how your database handles BETWEEN boundary values.

MySQL BETWEEN time and date

BETWEEN AND is often used to retrieve content within a time or date range. Here are some common BETWEEN time and date examples:

// int 时间戳格式,查询 2008-08-08 20:00:00 到 2009-01-01 零点之前的数据
SELECT * FROM table WHERE column_time BETWEEN 1218196800 AND 1230739199

// DATE 格式,查询 2008-08-08 到 2009-01-01 零点之前的数据
SELECT * FROM table WHERE column_time BETWEEN &#39;2008-08-08&#39; AND &#39;2009-01-01&#39;

// DATETIME 格式,查询 2008-08-08 20:00:00 到 2009-01-01 零点之前的数据
SELECT * FROM table WHERE column_time BETWEEN &#39;2008-08-08 20:00:00&#39; AND &#39;2008-12-31 23:59:59&#39;
Copy after login

But for queries For data at the current time, it is recommended to use the >= operator:

// DATETIME 格式,查询 2008-08-08 20:00:00 到当前时刻的数据
SELECT * FROM table WHERE column_time >= &#39;2008-08-08 20:00:00&#39;
Copy after login

It can be seen that the same requirements and different field types may be written in different ways. In terms of efficiency, the int timestamp format is the most efficient.

Although the above examples of BETWEEN are all SELECT queries, BETWEEN can also be used in SQL that applies WHERE expressions such as UPDATE and DELETE.

MySQL BETWEEN data comparison

BETWEEN also has a data comparison function, the syntax is as follows:

expr BETWEEN min AND max
Copy after login

When the value of the expr expression is greater than or equal to min and less than or equal to max, The return value of BETWEEN is 1, otherwise it returns 0. Using this function, you can determine whether an expression or value is in a certain range:

// 返回 0
SELECT 1 BETWEEN 2 AND 3
// 返回 1
SELECT &#39;b&#39; BETWEEN &#39;a&#39; AND &#39;c&#39;
// 判断日期范围
SELECT 20080808 BETWEEN 20080101 AND 20090101
Copy after login

BETWEEN has similar similarities to operators such as =, > in some cases. function, but BETWEEN has a higher computing level and is even more efficient. Of course, BETWEEN is not flexible enough due to boundary value issues, so different situations require specific treatment of which operator to use.

The above is the content of MySQL BETWEEN usage. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template