if语句中的case语句

时间:2013-10-23 08:53:07

标签: sql oracle case

没有经历过案例陈述,所以任何人都可以帮助我吗?

SELECT NAME ,
CASE ID ,
WHEN 1 THEN 'NAME EXISTS'
FROM EXAMPLE 
END;

这个简单的案例条件有效,但我需要添加if条件,如下所示:

SELECT X.NAME, E.TYPE ,
CASE X.ID ,
WHEN 1 THEN IF E.TYPE='BIG' THEN 'NAME EXISTS'
WHEN 2 THEN IF E.TYPE='SMALL' THEN 'NAME EXISTS'
WHEN 3 THEN IF E.TYPE='MEDIUM' THEN 'NAME EXISTS'
WHEN 4 THEN IF E.TYPE='MEDIUM' AND NAME='STACK' THEN 'NAME EXISTS'
ELSE 'IT DOESNT EXISTS'
FROM EXAMPLE X , EXTERNAL E

END;

1 个答案:

答案 0 :(得分:2)

 SELECT
   X.NAME, 
   E.TYPE,
   CASE WHEN X.ID = 1 AND E.TYPE = 'BIG' THEN 'NAME EXISTS'
        WHEN X.ID = 2 AND E.TYPE = 'SMALL' THEN 'NAME EXISTS'
        WHEN X.ID = 3 AND E.TYPE = 'MEDIUM' THEN 'NAME EXISTS'
        WHEN X.ID = 4 AND E.TYPE = 'MEDIUM' AND NAME = 'STACK' THEN 'NAME EXISTS'
        ELSE 'IT DOESNT EXISTS'
    END