1

我发现我们可以从Oracle具有批量输入的表中检索记录,如下所示:

SELECT 
    * 
FROM
    invoices
WHERE
    (invoice_id, entity_id) IN 
                               (
                                   (1, 101), 
                                   (2, 102), 
                                   (3, 102)
                               )

现在,我需要触发一个查询,其中一列的输入无关紧要,但仍然能够进行批量查询。

例如

SELECT 
    * 
FROM
    invoices
WHERE
    (invoice_id, entity_id, vendor_id) IN 
                               (
                                   (1, 101, 201), 
                                   (2, 102, 202), 
                                   -- no criteria on vendor_id for the tuple below
                                   (3, 102)   
                               )

如果我运行此查询,查询引擎会报告以下错误:

ERROR at line 1:
ORA-00920: invalid relational operator

是否可以首先进行这样的查询?如果是这样,怎么做?

4

2 回答 2

2

Oracle:表达式列表:

每个集合中的表达式数量必须与比较条件中运算符之前或成员条件中 IN 关键字之前的表达式数量匹配。

您可以将这些条件分开:

SELECT 
    * 
FROM
    invoices
WHERE
    (invoice_id, entity_id, vendor_id) IN 
                               (
                                   (1, 101, 201), 
                                   (2, 102, 202)
                               )
    or 
    (invoice_id, entity_id) IN 
                               (
                                   -- no criteria on vendor_id for the tuple below
                                   (3, 102)   
                               )
于 2012-08-31T06:49:05.657 回答
1
SELECT 
    * 
FROM
    invoices
WHERE
    (invoice_id, entity_id, vendor_id) IN 
                               (
                                   select 1, 101, 201 from dual
                                   union 
                                   select 2, 102, 202 from dual
                                   union
                                   select 3, 102, vendor_id from invoices
                               )
于 2012-08-31T06:54:02.607 回答