繁体   English   中英

是否可以使用SQL将值传递给order by子句?

[英]Is it possible to pass a value to an order by clause using SQL?

我对sql存储过程有奇怪的要求。 我有rqst,key和keyType表。 Rqst表列保存不同主表的FK,例如status,requesType和processType。 键表将rqstId保留为FK之一。 keyType是密钥表中使用的主表。

我的爸爸看起来像这样
RqstTable

rqstId appId statsId procesTypId requestType updtBy createDt  orchiveId
10125   3       102     5           4          Test    date    c1235a

键表

keyId rqstId keyTypeId KeyValue  
123   10125  2          9586  

键类型表

KeyTypeId KeyTypeName Description     
1           key1        key1des    
2           key2        key2des

我的要求是用户将提供这些输入字段status,requestType, processType and date 还应该order by key3 from keyTypeTable

根据我的搜索,我们不能将value传递给order by子句;根据文档,我们不能将expression传递为order by的一部分。 我累了scalr子查询

select * from rqst a,
left join key b on b.rqstId = a.rqstId
left join KeyType c on c.keyTypeId = b.KeyTypeId
where a.procesTypId = 5 and a.requestType = 4 and a.statsId = 102
order by KeyTypeName; -- but want some thing like KeyTypeName='key2'

但是结果与out order by子句相同。它没有用。

我的预期是

KeyValue  rqstId  orchiveId   
9586      10125   c1235a 

我为此使用Oracle写我的sql。

有人可以建议我吗,或者我可以要求这样的命令吗?

尝试这个:

select b.KeyValue,
       a.rqstId,
       a.orchiveId,
       case when c.KeyTypeName = 'key2' then 0 else 1 end as KeyTypeNameForOrder
  from rqst a, key b, keyType c
 where a.statusId = 102
   and a.requestType = 4
   and a.processTypeId = 5
   and b.rqstId = a.rqstId     
   and c.KeyTypeId = b.keyTypeId
 order by 4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM