oracle按照指定順序進行排序

來源:互聯網
上載者:User

標籤:

之前在網上查了下按照指定順序進行排序的方法,根據charindex來處理排序,但是在oracle發現不行,因為oracle沒有charindex函數,然後使用instr代替了charindex,然後又在網上搜了另外一種方

 

實驗如下:

1.建立表
CREATE TABLE BR_DICT(
ID number PRIMARY KEY NOT NULL,
D_ITEM VARCHAR2(32),
D_VALUE VARCHAR2(32),
D_TYPE VARCHAR2(32),
D_SORT number
)

2.插入資料

insert into br_dict(id, d_item,d_value,d_type,d_sort) values

(10, ‘10‘, ‘測試1‘, ‘‘, 0);
insert into br_dict(id, d_item,d_value,d_type,d_sort) values
(11, ‘11‘, ‘測試2‘, ‘‘, 1);
insert into br_dict(id, d_item,d_value,d_type,d_sort) values
(12, ‘12‘, ‘測試3‘, ‘‘, 2);
insert into br_dict(id, d_item,d_value,d_type,d_sort) values
(13, ‘13‘, ‘測試4‘, ‘‘, 3);
insert into br_dict(id, d_item,d_value,d_type,d_sort) values
(14, ‘14‘, ‘測試5‘, ‘‘, 4);
insert into br_dict(id, d_item,d_value,d_type,d_sort) values
(15, ‘15‘, ‘測試6‘, ‘‘, 5);

 

3.兩種查詢方式

a.instr函數在Oracle/PLSQL中是返回要截取的字串在源字串中的位置來自訂排序方式

select * from br_dict d where d.d_value like ‘%測試%‘ order by instr( ‘3,1,2,0,5,4‘,rtrim(cast(d_sort as nchar)));

b.運行decode定義某個字元的排序位置類似的實現排序方式

select * from br_dict d where d.d_value like ‘%測試%‘ order by decode(d_sort, ‘3‘, ‘01‘, ‘2‘, ‘02‘, ‘1‘, ‘03‘, ‘0‘, ‘04‘, ‘5‘, ‘05‘, ‘4‘, ‘06‘);

具體實現效率我資料量很小,沒去分析和實驗,有沒有大神分析的

oracle按照指定順序進行排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.