0

我有一个界面,用户可以在该界面上选择多个值:

输入 x:

   a , b , c , d .....(100 values for have an estimation ) 

输入 y:

   aa, bb, cc, dd .... (100 values for have an estimation )

我只有 1 个查询应该根据输入 x 和 y 改变(where 条件)。

例如,如果用户选择:

  a,b (for x)

  aa,bb

查询应该是:

 select * from Table where condition1 = a and condition2 = aa

 select * from Table where condition1 = b and condition2 = aa

 select * from Table where condition1 = a and condition2 = bb

 select * from Table where condition1 = b and condition2 = bb

所以它是所有组合的笛卡尔积......

所以我的问题是如何优化查询?我不知道有人告诉我使用 NVL 功能

http://www.techonthenet.com/oracle/functions/nvl.php你能帮我把它放在一个好的结构上吗?谢谢你

一件重要的事:

用户不能为其中一个输入选择任何内容

4

1 回答 1

0

你可以做condition1 in (x values list) and condition2 in (y values list)

尝试这个

select * from Table 
where (@x is null or condition1 IN ('a','b')) 
and (@y is null or condition2 in ('aa','bb'));

例子:

WITH CTE AS
(
  SELECT 'a' COL1, 'aa' COL2 FROM DUAL
  UNION ALL
  SELECT 'a' COL1, 'bb' COL2 FROM DUAL
   UNION ALL
  SELECT 'a' COL1, 'cc' COL2 FROM DUAL
   UNION ALL
  SELECT 'b' COL1, 'aa' COL2 FROM DUAL
  UNION ALL
  SELECT 'b' COL1, 'bb' COL2 FROM DUAL
   UNION ALL
  SELECT 'b' COL1, 'cc' COL2 FROM DUAL
   UNION ALL
  SELECT 'b' COL1, null COL2 FROM DUAL
)

select * from cte where col1 in ('a','b') and col2 in ('aa','bb');

SQL 演示

于 2013-05-20T16:00:17.607 回答