选择条件oracle

时间:2011-11-29 17:25:42

标签: sql oracle

我有一个VARCHAR,值aaa或bbb说 依赖于此我必须做出

  SELECT x FROM y WHERE y AND som = 1 

  SELECT x FROM y WHERE y AND som = 2

我有什么办法让这个像

SELECT i AS j,
case xxx
      when z then som1
      else som2
  end as ABC  
FROM ...

// EDIT

我想制作这样的东西(比如说它的伪代码;))

variable VARCHAR;

SELECT id FROM table WHERE age =
CASE variable
WHEN 'aaa' THEN 21
ELSE 'bbb' THEN 99
END

1 个答案:

答案 0 :(得分:5)

您可以使用CASE声明

SELECT id
  FROM table
 WHERE age = (CASE WHEN variable = 'aaa' 
                   THEN 21
                   WHEN variable = 'bbb'
                   THEN 99
                   ELSE null
                END)

但是,只需执行OR

,阅读效率可能会更高效,也更容易
SELECT id
  FROM table
 WHERE (variable = 'aaa' AND age = 21)
    OR (variable = 'bbb' AND age = 99)