oracle数据库查询通过一个待查询字段的值组装不同的条件
现在有个需求:
筛选框为应返机构,如果项目为拍卖项目,那么筛选框的应返机构需要匹配拍卖项目的应返机构字段值;如果项目为非拍卖项目,那么筛选框的应返机构需要匹配非拍卖项目的代理机构的字段值。其中拍卖项目和非拍卖项目保存在同一个表里面,用isAuction来区分,如果isAuction == 1则为拍卖项目,否则为非拍卖项目。
实现的sql(利用decode):
select uuid,
isAuction,
backMoney_total,
backMoney_currencyType,
backagent,
agentname,
decode(isAuction, 1, backAgent, agentname)
from spfeetradeback
where ((decode(isAuction, NULL, 1, 0) = 0) or agentname like '%天同证券%')
and ((decode(isAuction, NULL, 1, 0) = 1) or backAgent like '%天同证券%');
天同证券是筛选框的值
如果项目为拍卖项目,那么(decode(isAuction, NULL, 1, 0) = 0) 为true,则忽略后面的“or agentname like '%天同证券%'”,而(decode(isAuction, NULL, 1, 0) = 1)为false,则执行“backAgent like '%天同证券%'”。所以当项目为拍卖项目的时候,上面的sql就相当于:
select uuid,
isAuction,
backagent,
agentname,
decode(isAuction, 1, backAgent, agentname)
from spfeetradeback
where backAgent like '%天同证券%';
如果是非拍卖项目,那么(decode(isAuction, NULL, 1, 0) = 0)为false,则执行“agentname like '%天同证券%'”,而(decode(isAuction, NULL, 1, 0) = 1)为true,则忽略“backAgent like '%天同证券%'”。所以当项目为非拍卖项目的时候,上面的sql就相当于:
select uuid,
isAuction,
backagent,
agentname,
decode(isAuction, 1, backAgent, agentname)
from spfeetradeback
where agentname like '%天同证券%';
------>froest
爱情终将消失于茫茫的时间洪流之中,沉淀于厚重的黄泥沙丘之下...