【问题标题】:2 results from the case directive案例指令的 2 个结果
【发布时间】:2021-11-09 15:19:58
【问题描述】:

为什么在这个查询中我得到两个使用大小写的结果? 我以为在满足第一个条件之后,就会出现中断,而第二个根本不检查

select KOD from ap_magazyny mag where 1 =
    case 
    when (value1 like 'ZZ%' and mag.KOD like (select SUBSTR(value1,INSTR(value1,'/',1)+1,3) from dual)) then 1
    when (atrybut_t01 = value2 AND rownum = 1) then 1
    else 0
    end

结果喜欢

【问题讨论】:

  • 您有两行符合条件。有什么问题?
  • case 在列级别上运行。
  • 是否可以在匹配到第一个条件后打破 case 语句?
  • 这是两个单独的 KOD,匹配条件 ZG0 和 ZG1
  • @GeorgeJoseph In date like select KOD from ap_magazyny mag where 1 = case when (atrybut_t01 = 5902596218362 AND rownum = 1) then 1 when ('ZZT/ZG1/00023/07/2021' like 'ZZ%' and mag.KOD like (select SUBSTR('ZZT/ZG1/00023/07/2021',INSTR('ZZT/ZG1/00023/07/2021','/',1)+1,3) from dual)) then 1 else 0 end Are only 1 match value in first condition its ZG1

标签: sql oracle plsql


【解决方案1】:

我希望我的例子会有所帮助:

with cte as (select case when id = 1 then
                       1
                  else 
                       2
                  end A 
           from test
           where rownum = 1) 
select *
from  test
join cte on test.id = cte.A

您需要将它连接到某个东西,因为 1 永远是 1,您将从表测试中获得所有结果。

DEMO

【讨论】:

    【解决方案2】:

    您可以使用 rownum=1 来限制结果。

    select * from (
        select KOD 
          from ap_magazyny mag 
         where 1 = case when (value1 like 'ZZ%' 
                             and mag.KOD like (select SUBSTR(value1,INSTR(value1,'/',1)+1,3) 
                                                 from dual)
                                               ) then 1
                        when (atrybut_t01 = value2 AND rownum = 1) then 1
                        else 0
                   end
        order by case when (value1 like 'ZZ%' 
                             and mag.KOD like (select SUBSTR(value1,INSTR(value1,'/',1)+1,3) 
                                                 from dual)
                                               ) then 1
                        when (atrybut_t01 = value2 AND rownum = 1) then 2
                   end
    )x
    where rownum=1
    

    【讨论】:

    • 使用 rownum 它会根据数据库放在最前面的结果获得随机结果。不幸的是,结果的顺序不遵循案例顺序
    • 假设只有 value2 导致两个条目应该是什么顺序
    • 更新了按最先满足的条件排序的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多