在oracle数据库中使用筛选搜索查询

时间:2017-06-06 02:11:56

标签: oracle

SELECT * FROM EMPLOYEES
  WHERE (1 = 1 OR FIRST_NAME = 'renz')
    AND (1 = 1 OR LAST_NAME LIKE '%t%')
    AND (1 = 1 OR EMAIL = 'ASD')
    AND
    (CASE 
      WHEN ('06/20/2016'!='' AND '01/23/2017'!='')
        THEN HIRE_DATE BETWEEN TO_DATE('06/20/2016','YYYYMMDD') AND TO_DATE('01/23/2017','YYYYMMDD')
      WHEN ('06/20/2016'!='' AND '01/23/2017'=='')
        THEN HIRE_DATE >=TO_DATE('06/20/2016','YYYYMMDD')
      WHEN ('06/20/2016'=='' AND '01/23/2017'!='')
        THEN HIRE_DATE <=TO_DATE('01/23/2017','YYYYMMDD')
    END)

我正在尝试创建一个将搜索的搜索查询:firstname完全匹配,lastname部分匹配,电子邮件完全匹配,日期和日期之间的数据  如果from和to都存在,它将在这些日期之间返回数据  如果from是唯一存在,它将返回日期大于from的数据  if if是唯一存在的,它将返回日期小于迄今的数据 我怎样才能实现这一目标?我的过滤数据来自我的jsp页面传递给我的DAO类

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

SELECT * FROM EMPLOYEES
  WHERE (1 = 1 OR FIRST_NAME = 'renz')
    AND (1 = 1 OR LAST_NAME LIKE '%t%')
    AND (1 = 1 OR EMAIL = 'ASD')
    AND HIRE_DATE BETWEEN NVL(TO_DATE(:FROM_DT, 'YYYYMMDD'), HIRE_DATE) 
                      AND NVL(TO_DATE(:TO_DT,'YYYYMMDD'), HIRE_DATE)
;

答案 1 :(得分:0)

使用以下查询:

SELECT *
  FROM employees e
 WHERE e.first_name = NVL( :first_name, e.first_name )
   AND e.last_name LIKE NVL( :last_name, e.last_name )
   AND e.email = NVL( :email, e.email )
   AND e.hire_date BETWEEN NVL( :start_date, e.hire_date )
                       AND NVL( :end_date, e.hire_date )

可以使用零个或多个参数执行查询。