两个列表之间的 IN 运算符在 oracle 中不起作用
IN operator between two lists is not working in oracle
当我在没有 IN 运算符的情况下使用时,其工作方式如下
SELECT * FROM range_name_t where (cty_code_iso='CN' and lang_code_iso='zh');
但是当我如下使用 IN 运算符时得到 ORA-00920:无效的关系运算符错误
SELECT * FROM range_name_t where ( cty_code_iso, lang_code_iso ) IN ( 'CN','zh' );
ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
如何让它在第二个查询之上工作?
我没有你的表格,但我有 Scott 的 EMP 来演示它:
SQL> select *
2 from emp
3 where (deptno, job) in (select 10, 'CLERK' from dual);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23.01.82 1300 10
SQL>
根据 Oracle documentation of the IN
condition:
in_conditions::=
expression_list::=
If you use the upper form of the in_condition
condition (with a single expression to the left of the operator), then you must use the upper form of expression_list
. If you use the lower form of this condition (with multiple expressions to the left of the operator), then you must use the lower form of expression_list
, and the expressions in each expression_list must match in number and datatype the expressions to the left of the operator.
您正在使用语法的“低级”版本并匹配 IN
条件左侧的值元组;因此右侧必须是元组列表,您需要使用第二组括号(列表的外括号和列表中每个元组周围的内括号):
SELECT *
FROM range_name_t
where ( cty_code_iso, lang_code_iso ) IN (( 'CN','zh' ));
其中示例数据:
CREATE TABLE range_name_t ( cty_code_iso, lang_code_iso ) AS
SELECT 'CN', 'zh' FROM DUAL UNION ALL
SELECT 'CN', 'ab' FROM DUAL UNION ALL
SELECT 'IT', 'zh' FROM DUAL;
输出:
CTY_CODE_ISO | LANG_CODE_ISO
:----------- | :------------
CN | zh
db<>fiddle here
SELECT * FROM range_name_t
WHERE cty_code_iso IN ('CN') AND lang_code_iso IN ('zh');
我认为您的主要示例查询没有任何问题。有没有理由必须使用“IN”?到目前为止的所有答案(包括这个)都在巧妙使用“IN”和滥用“IN”之间划清界限。
当我在没有 IN 运算符的情况下使用时,其工作方式如下
SELECT * FROM range_name_t where (cty_code_iso='CN' and lang_code_iso='zh');
但是当我如下使用 IN 运算符时得到 ORA-00920:无效的关系运算符错误
SELECT * FROM range_name_t where ( cty_code_iso, lang_code_iso ) IN ( 'CN','zh' );
ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
如何让它在第二个查询之上工作?
我没有你的表格,但我有 Scott 的 EMP 来演示它:
SQL> select *
2 from emp
3 where (deptno, job) in (select 10, 'CLERK' from dual);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23.01.82 1300 10
SQL>
根据 Oracle documentation of the IN
condition:
in_conditions::=
expression_list::=
If you use the upper form of the
in_condition
condition (with a single expression to the left of the operator), then you must use the upper form ofexpression_list
. If you use the lower form of this condition (with multiple expressions to the left of the operator), then you must use the lower form ofexpression_list
, and the expressions in each expression_list must match in number and datatype the expressions to the left of the operator.
您正在使用语法的“低级”版本并匹配 IN
条件左侧的值元组;因此右侧必须是元组列表,您需要使用第二组括号(列表的外括号和列表中每个元组周围的内括号):
SELECT *
FROM range_name_t
where ( cty_code_iso, lang_code_iso ) IN (( 'CN','zh' ));
其中示例数据:
CREATE TABLE range_name_t ( cty_code_iso, lang_code_iso ) AS
SELECT 'CN', 'zh' FROM DUAL UNION ALL
SELECT 'CN', 'ab' FROM DUAL UNION ALL
SELECT 'IT', 'zh' FROM DUAL;
输出:
CTY_CODE_ISO | LANG_CODE_ISO :----------- | :------------ CN | zh
db<>fiddle here
SELECT * FROM range_name_t
WHERE cty_code_iso IN ('CN') AND lang_code_iso IN ('zh');
我认为您的主要示例查询没有任何问题。有没有理由必须使用“IN”?到目前为止的所有答案(包括这个)都在巧妙使用“IN”和滥用“IN”之间划清界限。