Skip to content

NOT BETWEEN ... AND ...

Grammar Description

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

Grammar Structure

> expr NOT BETWEEN min AND max

Example

mysql> SELECT 2 NOT BETWEEN 1 AND 3, 2 NOT BETWEEN 3 and 1;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 2 not between 1 and 3 | 2 not between 3 and 1 |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| false | true |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 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 not between 8894754949779693574 and 17790886498483827171;
+-------------+
| count(*) |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)