时间:2021-07-01 10:21:17 帮助过:17人阅读
expr 表示要判断的表达式,value1, value2, value3 ... valueN 表示列表中的值。MySQL 会将 expr 的值和列表中的值逐一对比。
【实例1】在 SQL 语句中使用 IN 和 NOT IN 运算符:
mysql> SELECT 2 IN (1,3,5,‘thks‘),‘thks‘ IN (1,3,5, ‘thks‘); +---------------------+---------------------------+ | 2 IN (1,3,5,‘thks‘) | ‘thks‘ IN (1,3,5, ‘thks‘) | +---------------------+---------------------------+ | 0 | 1 | +---------------------+---------------------------+ 1 row in set, 2 warnings (0.00 sec) 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)
由结果可以看到,IN 和 NOT IN 的返回值正好相反。
当 IN 运算符的两侧有一个为空值 NULL 时,如果找不到匹配项,则返回值为 NULL;如果找到了匹配项,则返回值为 1。
请看下面的 SQL 语句如下:
mysql> SELECT NULL IN (1,3,5,‘thks‘),10 IN (1,3,NULL,‘thks‘); +------------------------+-------------------------+ | NULL IN (1,3,5,‘thks‘) | 10 IN (1,3,NULL,‘thks‘) | +------------------------+-------------------------+ | NULL | NULL | +------------------------+-------------------------+ 1 row in set, 1 warning (0.00 sec) mysql> SELECT NULL IN (1,3,5,‘thks‘),10 IN (1,10,NULL,‘thks‘); +------------------------+--------------------------+ | NULL IN (1,3,5,‘thks‘) | 10 IN (1,10,NULL,‘thks‘) | +------------------------+--------------------------+ | NULL | 1 | +------------------------+--------------------------+ 1 row in set (0.00 sec)
NOT IN 恰好相反,当 NOT IN 运算符的两侧有一个为空值 NULL 时,如果找不到匹配项,则返回值为 NULL;如果找到了匹配项,则返回值为 0。
请看下面的 SQL 语句如下:
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教程58-IN和NOT IN用法详解
标签:style color 找不到 weight 结果 mys warnings cheng lan