首页 > 解决方案 > 基于参数的select语句中的条件列

问题描述

我有以下条件:

- If ouk_shipment.shipper_parent_id = Parameter1 OR
ouk_shipment.shipper_id = Parameter2
=====> ouk_shipment.first_mwb_ref_id
- If ouk_shipment.cnee_parent_id = Parameter1 OR
ouk_shipment.cnee_id = Parameter2
=====> ouk_shipment.last_mwb_ref_id

**Parameter1 和Parameter2:我们在SQL 运行时输入。

我试图在这里实现以下目标:

Select case when condition 1=True then ouk_shipment.first_mwb_ref_id
            when condition 2=True then ouk_shipment.last_mwb_ref_id End AS Col1
From ouk_shipment

如何在我的 SQL 中添加条件?请检查

标签: sqloracleselectparametersconditional-statements

解决方案


你可以做 - 注意我做了 'XXX' 假设它是 varchar,你可以将它更改为 Integer for Int

SELECT CASE WHEN  ouk_shipment.shipper_parent_id = COALESCE(Parameter1,'XXX') 
                      OR ouk_shipment.shipper_id = COALESCE(Parameter2,'YYYY')
              THEN ouk_shipment.first_mwb_ref_id
            WHEN ouk_shipment.shipper_parent_id = COALESCE(Parameter1,'XXX') 
                OR ouk_shipment.shipper_id = COALESCE(Parameter2,'YYYY')
              THEN ouk_shipment.last_mwb_ref_id End AS Col1
FROM ouk_shipment

推荐阅读