sql - 基于参数的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 中添加条件?请检查
解决方案
你可以做 - 注意我做了 '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
推荐阅读
- javascript - 我无法使用 .push() 函数和使用 node.js 的 firebase 推送数据
- python - 未知循环数的嵌套循环
- python - Barplot 似乎没有正确排序我的 df
- c - 使可变长度输入参数无效的 C 宏
- android - 如何从 Android 中的前台服务发送通知
- sql - 更新查询包含特殊字符
- javascript - 如何使用 javascript 在 excel/office 中复制图像,因为 navigator.clipboard 不可用?
- r - 为什么部分代码块运行而不显示结果?
- python - 如何根据百分比更改颜色,使用 plotly choropleth 的美国地图?
- javascript - 使用 Typescript 时增加 React 状态