Skip to content

BETWEEN ... AND ...

Grammar Description

BETWEEN ... AND ... operator selects values ​​within the data range between two values. These values ​​can be numerical values, texts, or dates. If the value is between two values, then return true, otherwise return the result is false.

Grammar Structure

> expr BETWEEN min AND max

Example

mysql> SELECT 2 BETWEEN 1 AND 3, 2 BETWEEN 3 and 1;
+----------------------------------------------------------+
| 2 between 1 and 3 | 2 between 3 and 1 |
+----------------------------------------------------------+
| true | false |
+----------------------------------------------------------+
1 row in set (0.01 sec)
create table t (id bigint unsigned, b int);
insert into t values(8894754949779693574,1);
insert into t values(8894754949779693579,2);
insert into t values(17790886498483827171,3);

mysql> select count(*) from t where id>=8894754949779693574 and id =17790886498483827171 order by 1 asc;
+-------------+
| count(*) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)

mysql> select count(*) from t where id between 8894754949779693574 and 17790886498483827171;
+-------------+
| count(*) |
+-------------+
| 3 |
+-------------+
1 row in set (0.00 sec)