如何在oracle中使用CASE的地方使用查询

How to use query where using CASE in oracle

您好,我正在尝试 运行 这个查询,但是在 where 语句中,Case 给出了一个错误。我想做的是,if i_id is >= 284 then append cnint <=65 to the query after the and else append the other one.此查询在存储过程中。

Select cnint as Id, cnabbv as Code, rtrim(cnname) as Name, i_id as CId, cntry as CCode 
from money.country where cntype = 1 and (CASE 
                                             WHEN i_id = 284 THEN cnint <= 65
                                             ELSE cnint > 65 
                                         END)
order by cnname;

此查询在存储过程中。

那里不支持。试试这个:

Select cnint as Id, cnabbv as Code, rtrim(cnname) as Name, i_id as CId, cntry as CCode 
from money.country where cntype = 1 and
((i_id = 284 AND cnint <= 65) OR (coalesce(i_id, 0) != 284 and cnint > 65))
order by cnname;

你可以不用 case 像这样

select cnint as Id, 
       cnabbv as Code, 
       rtrim(cnname) as Name, 
       i_id as CId, 
       cntry as CCode 
from money.country 
where cntype = 1 and ( ( i_id = 284 AND cnint <= 65 ) OR (coalesce(i_id, 0) != 284 and cnint>65) )
order by cnname;

作为 case returns 标量值,您可以像这样使用 case 来做同样的事情 但是没有 case

更易读
   select cnint as Id, 
           cnabbv as Code, 
           rtrim(cnname) as Name, 
           i_id as CId, 
           cntry as CCode 
    from money.country 
    where cntype = 1  AND 1 = (CASE WHEN (i_id = 284 AND cnint <= 65 ) OR (coalesce(i_id, 0) != 284 and cnint >65)  
                                    THEN 1
                                    ELSE 0 
                               END)
order by cnname;