如果Else更改了Oracle中的where子句

时间:2014-04-29 15:06:06

标签: oracle if-statement where

我试图根据用户在Japser iReport网络界面上选择的值为我的WHERE子句添加一行。

Select KeyId, Name, WorkOrder
FROM orderTbl oT
WHERE

------- ????????????????? --------------

If userSelectedValue = 'Load'
        oT.workOrder = 'accepted'
End If

------- ????????????????? --------------

我尝试按照CASE示例,但是会创建一个包含结果的新列,但不知道如何使用新值来获取我想要的记录。

Select KeyId, Name, WorkOrder
case 
  when userSelectedValue = 'Load' and workOrder  ='accepted' then 'useWorkOrder'
     else 'noWorkOrder'
end as "dropDown",

dropDown          KeyId  Name           WorkOrder
----------------  -----  -------------  --------------
useWorkOrder      45     John Doe       accepted
useWorkOrder      78     Bill Kearns    accepted
noWorkOrder       23     Sandy York     null

My Final Goal:
Retrieve 2 records if user selects "Load" from a drop-down (userSelectedValue="Load")
- 45     John Doe 
- 78     Bill Kearns
Retrieve 1 record if user selects "Retrieve" from a drop-down (userSelectedValue="Retrieve")
- 23     Sandy York

3 个答案:

答案 0 :(得分:0)

您不必使用CASE。您可以使用复合条件:

SELECT KeyId, Name 
FROM orderTbl
WHERE (userSelectedValue = 'Load' and workOrder = 'accepted' AND DropDown = 'useWorkOrder')
OR    (DropDown = 'noWorkOrder');

答案 1 :(得分:0)

您可以做的最简单的修改是:

Select KeyId, Name 
WHERE case 
  when userSelectedValue = 'Load' and workOrder  ='accepted' then 'useWorkOrder'
     else 'noWorkOrder'
end = 'useWorkOrder'

答案 2 :(得分:0)

<强>解决方案:

我使用了所有想法的组合来实现这一目标。特别感谢Joseph B + gubble。

<强> 问题:

Select KeyId, Name, WorkOrder
FROM orderTbl oT
WHERE
If userSelectedValue = 'Load'
        oT.workOrder = 'accepted'
End If

<强> 答案:

Select KeyId, Name, WorkOrder
FROM orderTbl oT
WHERE
 case 
    when (oT.KeyId = userSelectedValue AND oT.workOrder = 'accepted'
          OR de.KeyId = userSelectedValue)
       then 'useWorkOrder'
       else 'noWorkOrder'
 end = 'useWorkOrder'