【问题标题】:Oracle query with multiple IN clauses with more than 1000 values具有超过 1000 个值的多个 IN 子句的 Oracle 查询
【发布时间】:2018-01-03 15:13:10
【问题描述】:

我们是否可以使用多个 IN 子句进行 oracle 查询,每个子句可以有 1000 多个值?

select * from student where name in(1000+ values) and id in (1000+ values)

我已经看到其他线程具有相同类型的问题,带有单个 IN 子句和解决方案临时表

谢谢 -拉纳德

【问题讨论】:

  • 这对我来说是糟糕的设计。如果两者都是 1:1(即一个名称与一个 Id 相关联),则创建多个临时表或一个具有多个列的临时表。您不想定期将 40kb 的 select - 语句传递给您的数据库....
  • MySQL 的 IN 子句可以容纳比 Oracle 更多的文字。您使用的是哪个 DBMS?请edit你的标签。
  • 或者您可以根据需要只OR 尽可能多的IN(less than 1000)
  • 非常糟糕的设计。如有必要,创建一个临时表并制作where name in (select name from temp_table)
  • 我不能做OR,应该是AND,因为这个查询是基于UI过滤器选择的,过滤器是AND过滤器

标签: oracle


【解决方案1】:

IN 条件在另一个词中表示OR,例如:

selecT * from table where col1 in('A','B','C') 和它一样 selecT * from table where col1 ='A' or col1 ='B' or col1 ='C' 所以想象一下你正在向 1000+ 表达式添加 OR,这对性能和设计来说真的很糟糕,为什么你需要使用它?作为替代方案,您可以创建一个临时表并在其中添加值并从中进行选择

create table_tmp (SOME_EXPRESSION varchar2(1000) null)
insert into table_tmp  (SOME_EXPRESSION ) values ('some condition');
commit;

 select * from tab1 where col1 in (select SOME_EXPRESSION  from table_tmp )

【讨论】:

    【解决方案2】:

    为了回答您的问题,截至Oracle 12.2,限制仍然存在:

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

    expression_list 中最多可以指定 1000 个表达式。

    如果临时表是唯一的问题,您必须注意,1,000 项限制仅适用于单个 IN() 条件,因此您始终可以根据需要随时 OR

    select *
    from student
    where 
        (name in(1, 2, …, 1000) or name in(1001, 1002, …, 2000) or …)
        and (id in (1, 2, …, 1000) or id in (1001, 1002, …, 2000) or …)
    

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      相关资源
      最近更新 更多