一. [NOT] BETWEEN..AND…
[NOT] BETWEEN..AND:[不]在范围之内
mysql> SELECT 2 BETWEEN 1 AND 3; +-------------------+ | 2 BETWEEN 1 AND 3 | +-------------------+ | 1 | +-------------------+ 1 row in set (0.00 sec) mysql> SELECT 6 BETWEEN 1 AND 3; +-------------------+ | 6 BETWEEN 1 AND 3 | +-------------------+ | 0 | +-------------------+ 1 row in set (0.00 sec) mysql> SELECT 2 NOT BETWEEN 1 AND 3; +-----------------------+ | 2 NOT BETWEEN 1 AND 3 | +-----------------------+ | 0 | +-----------------------+ 1 row in set (0.00 sec) mysql> SELECT 6 NOT BETWEEN 1 AND 3; +-----------------------+ | 6 NOT BETWEEN 1 AND 3 | +-----------------------+ | 1 | +-----------------------+ 1 row in set (0.00 sec) mysql> SELECT 6 BETWEEN 1 AND 6; +--------------------+ | 6 BETWEEN 1 AND 6 | +--------------------+ | 1 | +--------------------+ 1 row in set (0.00 sec)
二. [NOT] IN
[NOT] IN():[不]在列出值范围内,比如5、10、15、20这几个数字中有没有10这个数字呢?
mysql> SELECT 10 IN(5,10,15,20); +-------------------+ | 10 IN(5,10,15,20) | +-------------------+ | 1 | +-------------------+ 1 row in set (0.00 sec)
而如果是13的话:
mysql> SELECT 11 IN(5,10,15,20); +-------------------+ | 11 IN(5,10,15,20) | +-------------------+ | 0 | +-------------------+ 1 row in set (0.00 sec)
三. IS [NOT] NULL
IS [NOT] NULL:[不]为空
mysql> SELECT NULL IS NULL; +--------------+ | NULL IS NULL | +--------------+ | 1 | +--------------+ 1 row in set (0.00 sec) mysql> SELECT NOT NULL IS NULL; +------------------+ | NOT NULL IS NULL | +------------------+ | 0 | +------------------+ 1 row in set (0.00 sec) mysql> SELECT '' IS NULL; +------------+ | '' IS NULL | +------------+ | 0 | +------------+ 1 row in set (0.00 sec)
还可以用于查询字段,比如查询用户名为空的用户id:
mysql> SELECT * FROM test; +----+----------+ | id | username | +----+----------+ | 1 | John | | 2 | John | | 3 | 111 | | 4 | tom% | | 5 | NULL | | 6 | NULL | +----+----------+ 6 rows in set (0.00 sec) mysql> SELECT * FROM test WHERE username IS NULL; +----+----------+ | id | username | +----+----------+ | 5 | NULL | | 6 | NULL | +----+----------+ 2 rows in set (0.00 sec)
请登录之后再进行评论