Oracle选择查询条件

时间:2012-11-22 04:03:48

标签: sql oracle

我有一个搜索jsp,它有多个标准(ID或NAME OR DATE OR ...)来搜索数据库。我需要一个选择查询来满足所有条件。我已经部分编写了选择查询,但它运行不正常。请帮我完成查询

   select * from table 1
   where Id in ( select id from table 1 where ( those three conditions here)

   i have (1=? or id=?)
         and (1=? or name=?)
         and (1=? or date =?)

但它返回完整的表格。我知道这三个条件都返回true。如何修改它以获得结果。我不想要任何存储过程,因为我是新手

3 个答案:

答案 0 :(得分:1)

如果您的条件适用于每一行,您将获得完整的表格。

例如,如果您的变量为1,则1 = 1为真,并且每行都会返回。


p,s,我不明白为什么你嵌套了选择。

答案 1 :(得分:0)

select * from table 1
   where Id in ( select 
         (case 
               when (1=? or id=?) and (1=? or name=? )  and (1=? or date =?) then id
          end) as id from table 1 )

我希望这就是你要找的东西

答案 2 :(得分:0)

你有一个冗余的子查询。

如果第二个参数为null,我假设第一个参数是1,如果第二个参数不为空,我假设0;同样,如果第4个参数为空,第3个参数将为1,如果第4个参数不为空,则为0,等等?

试试这个:

select * from table
where (1=? or id=?)
     and (1=? or name=?)
     and (1=? or date =?)

例如,如果用户按名称搜索,我希望参数类似于(1,NULL,0,'SMITH',1,NULL)

(另外,您不能将1用于表别名。)

相关问题