Home > Database > Mysql Tutorial > Is MySQL's `BETWEEN` Clause Inclusive of the Upper Bound?

Is MySQL's `BETWEEN` Clause Inclusive of the Upper Bound?

DDD
Release: 2025-01-05 05:18:40
Original
517 people have browsed it

Is MySQL's `BETWEEN` Clause Inclusive of the Upper Bound?

MySQL "between" Clause: Inclusivity Considered

While utilizing the "between" clause in MySQL queries, it's essential to consider its non-inclusive nature regarding the ending value. For instance, executing a query like:

select * from person where dob between '2011-01-01' and '2011-01-31'
Copy after login

would retrieve results with dates of birth (DOB) ranging from '2011-01-01' to '2011-01-30', excluding records with a DOB of '2011-01-31'.

This behavior can be attributed to MySQL's interpretation of the "between" clause as a logical expression resembling:

(min <= expr AND expr <= max)
Copy after login

To account for this and include records where DOB is '2011-01-31', the query can be modified in two ways:

Method 1: Using Comparison Operators

By employing comparison operators, the query can explicitly check for both upper and lower bounds:

select * from person where dob >= '2011-01-01' and dob <= '2011-01-31'
Copy after login

Method 2: Utilizing the NOT BETWEEN Operator

Alternatively, the "NOT BETWEEN" operator can be used to exclude a specific range while including the desired endpoint:

select * from person where dob NOT BETWEEN '2011-01-02' and '2011-01-30'
Copy after login

The above is the detailed content of Is MySQL's `BETWEEN` Clause Inclusive of the Upper Bound?. For more information, please follow other related articles on the PHP Chinese website!

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