【问题标题】:Case condition in a where clause having another sql statement具有另一个 sql 语句的 where 子句中的 case 条件
【发布时间】:2013-05-20 21:21:17
【问题描述】:

我正在编写一份 jasper 报告,该报告采用一个参数,根据该参数应该有一个不同的 where 子句。以下是我正在使用的查询,但似乎某处存在语法错误。

  select customer.name
         customer_order.name
  from customer, customer
  where customer.orders > 0
    and customer_order.customer_id = customer.id
    and customer_order.name in
        (
           case when (<paramter> = 1) then ('order1', 'order2')
           else  (select order_name from customer_order)
           end
         );

感谢您的帮助, 谢谢

【问题讨论】:

    标签: sql oracle jasper-reports case


    【解决方案1】:

    你不能这样写查询。这是一个替代方案:

    where . . .
          ((<parameter> = 1) and customer_order.name in ('order1', 'order2') or
           (<parameter> <> 1) and customer_order.name in (select order_name from customer_order)
          )
    

    这是假设&lt;parameter&gt; 不为NULL。如果允许,您需要将其添加到逻辑中。

    【讨论】:

      【解决方案2】:

      您可以使用参数作为占位符动态创建查询的任何部分。

      1) 假设您有一个参数 param1,它是实际参数

      2) 创建另一个参数,比如 param2,它具有类似的 epression $P{param1}.longValue () == 1 ? " 和字段 1 = " : " 和 field1 = "

      3) 在查询中使用 param2 作为占位符

          Select 
              blah blah
          From 
              blah blha
          Where
              blah blah
              $P!{param2}
      

      现在,取决于你 where 子句可能看起来像的 param1 值

              Where 
                  blah blah
                  and field1=  <exp1>
              or 
      
               Where 
                  blah blah
                  and field1=  <exp2>
      

      【讨论】:

        猜你喜欢
        • 2020-03-26
        • 1970-01-01
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 2021-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多