云数据库OceanBase入门教程 OceanBase IN 条件

2024-02-26 开发教程 云数据库OceanBase入门教程 匿名 7

IN条件(IN Conditions)是成员资格条件。它测试值或子查询列表中成员的值。

语法

expr [ NOT ] IN  ({ expression_list | subquery })
|
( expr [, expr ]... ) [ NOT ] IN ( { expression_list [, expression_list ]... | subquery } )

IN条件可以测试表达式是表达式列表或子查询的成员,或者多个表达式是表达式列表或子查询的成员。并且每个表达式列表中的表达式必须在数量和数据类型上与运算符 IN左边的表达式匹配。

示例

IN 示例:相当于 =ANY,表示集合中所有的成员。

SELECT * FROM employees WHERE job_id IN ('PU_CLERK','SH_CLERK') ORDER BY employee_id;
SELECT * FROM employees WHERE salary IN (SELECT salary FROM employees
WHERE department_id =30) ORDER BY employee_id;

NOT IN 示例:相当于 != ALL。如果集合中的任何成员为 NULL,则计算为 false

SELECT * FROM employees WHERE salary NOT IN (SELECT salary FROM employees
WHERE department_id = 30) ORDER BY employee_id;
SELECT * FROM employees WHERE job_id NOT IN ('PU_CLERK', 'SH_CLERK')
ORDER BY employee_id;