簡體   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