是否有类似于case语句但返回多行可以在SELECT查询中使用?

时间:2014-06-12 11:49:55

标签: sql performance oracle select

基于输入" include_child'我可以选择哪里条件?

实际查询:

SELECT *
  FROM table1 t
WHERE t.node_id IN (SELECT decode(:include_Child , 1 ,child_node_id, 0 , 123) 
                             FROM table2 
                            WHERE node_id IN 123 )

寻找类似的东西:

IF include_Child = 0 then
SELECT *
  FROM table1 t
WHERE t.node_id IN 123 -- do no use table2 at all

-- only when condition met then use table2
IF include_Child = 1 then
SELECT *
  FROM table1 t
WHERE t.node_id IN (select child_node from table2)

我想在SELECT查询中使用它。不在PL / SQL块中。 DB版本是11g

有什么建议吗?

提前致谢!

2 个答案:

答案 0 :(得分:2)

如何使用andor

SELECT *
FROM table1 t
WHERE (:include_Child = 0 and t.node_id = 123) or
      (:include_Child = 1 and t.node_id IN (select child_node from table2) )

答案 1 :(得分:1)

这是另一种方法。

SELECT *
FROM table1 t
WHERE t.node_id IN 123 -- do no use table2 at all
AND include_Child = 0
UNION ALL
SELECT *
FROM table1 t
WHERE t.node_id IN (select child_node from table2)
AND include_Child = 1