温馨提示  2024年 6月我们已经停止开发者板块文章内容更新,谢谢来访。存档数据
知识 2023-08-28 31 次阅读

mysql中not in的用法是什么

   

在mysql中,“not in”用于判断表达式的值是否不存在于给出的列表中,语法为“expr not in(value1,value2,...)”;如果表达式的值不存在指定列表中,返回结果是1,否则返回结果是0。

本教程操作环境windows10系统、mysql8.0.22版本、dell g3电脑。

mysql中not in的用法是什么

not in 用来判断表达式的值是否不存在于给出的列表中;如果不是,返回值为 1,否则返回值为 0。

语法格式如下

expr not in ( value1, value2, value3 ... valuen )

expr 表示要判断的表达式,value1, value2, value3 ... valuen 表示列表中的值。mysql 会将 expr 的值和列表中的值逐一对比。

示例如下

mysql> select 2 not in (1,3,5,'thks'),'thks' not in (1,3,5, 'thks');+-------------------------+-------------------------------+| 2 not in (1,3,5,'thks') | 'thks' not in (1,3,5, 'thks') |+-------------------------+-------------------------------+| 1 | 0 |+-------------------------+-------------------------------+1 row in set, 2 warnings (0.00 sec)

当 not in 运算符的两侧有一个为空值 null 时,如果找不到匹配项,则返回值为 null;如果找到了匹配项,则返回值为 0。

示例如下

mysql> select null not in (1,3,5,'thks'),10 not in (1,0,null,'thks');+----------------------------+-----------------------------+| null not in (1,3,5,'thks') | 10 not in (1,0,null,'thks') |+----------------------------+-----------------------------+| null | null |+----------------------------+-----------------------------+1 row in set, 1 warning (0.00 sec)mysql> select null not in (1,3,5,'thks'),10 not in (1,10,null,'thks');+----------------------------+------------------------------+| null not in (1,3,5,'thks') | 10 not in (1,10,null,'thks') |+----------------------------+------------------------------+| null | 0 |+----------------------------+------------------------------+1 row in set (0.00 sec)

推荐学习mysql视频教程