oracle中REGEXP_SUBSTR方法的使用

來源:互聯網
上載者:User

標籤:dcl   javascrip   位置   問題:   and   this   length   問題   相同   

近期在做商旅機票平台,遇到這樣一個問題:
有一張tt_ticket表,用來存機票資訊。裡邊有一個欄位叫schedule,表示的是行程,儲存格式為:北京/虹橋
由於公司位於上海。而上海眼下有兩個機場:浦東和虹橋。所以對於出發地或到達地為上海的機票來說,行程中會存虹橋或浦東,當然。有時候可能也會直接存上海(可能性非常小。但不代表沒有)。這樣,行程對於出發地為上海的行程來說。可能有下面幾種可能:
a.虹橋/北京
b.浦東/北京
c.上海/北京
如今要實現搜尋出發城市為上海時,把這三條資訊所有拉出來。
首先,建立一張城市地區映射表tt_ticket_city_mapper,包括欄位city和mapper_city,儲存下面資料:

city mapper_city
虹橋 上海
浦東 上海
上海 上海

下面先把sql貼出來,是mybatis中的部分代碼:

      select *       from tt_ticket       where 1=1               <if test="departureCity !=‘‘ and departureCity !=null"><![CDATA[  and (nvl(         (select   m.mapper_city           from    tt_ticket_city_mapper m           where   m.city  = trim((select                   REGEXP_SUBSTR(t.schedule,‘[^/]+‘,1,1)           from dual))) ,     trim((select REGEXP_SUBSTR(t.schedule,‘[^/]+‘,1,1) from dual))    )     =    nvl(       (select m.mapper_city         from   tt_ticket_city_mapper m         where  m.city = trim(#{departureCity})       ),         #{departureCity}       )      )]]></if><if test="arriveCity !=‘‘ and arriveCity !=null"> <![CDATA[  and(nvl(       (select   m.mapper_city         from    tt_ticket_city_mapper m          where   m.city  = trim((select REGEXP_SUBSTR(t.schedule,‘[^/]+‘,1,2) from dual))        ) ,        trim((select REGEXP_SUBSTR(t.schedule,‘[^/]+‘,1,2) from dual))   )      =    nvl(       (select m.mapper_city         from   tt_ticket_city_mapper m         where  m.city = trim(#{arriveCity})                  ),         #{arriveCity}        )]]></if>

先解釋一下:select REGEXP_SUBSTR(t.schedule,’[^/]+’,1,1) from dual
比方:
select REGEXP_SUBSTR(‘虹橋/北京’,’[^/]+’,1,1) from dual
返回的是虹橋
select REGEXP_SUBSTR(‘虹橋/北京’,’[^/]+’,1,2) from dual
返回的是北京
第一個參數是要解析的字串,第二個是正在運算式,第三個表示字串解析時的起始位置。比方:
select REGEXP_SUBSTR(‘虹橋/北京’,’[^/]+’,2,1) from dual
返回的是橋
第四個參數表示取拆分後數組裡的第幾個值。
這種話
nvl(
(select m.mapper_city
from tt_ticket_city_mapper m
where m.city = trim((select REGEXP_SUBSTR(t.schedule,’[^/]+’,1,1) from dual))
) ,
trim((select REGEXP_SUBSTR(t.schedule,’[^/]+’,1,1) from dual))
)
就能夠取出/前面的字串,比方浦東或虹橋,然後在 tt_ticket_city_mapper中查詢得到器映射的城市上海。假設沒有的話就是它自己
等號後邊相同是做一下轉換。


當然,看到這,肯定會有人說為什麼不直接存出發城市和到達城市兩個欄位?那是由於行程可能是 北京/香港/邁阿密/香港/北京 這種資料。


關於這種資料怎麼處理。還沒有搞,只是在網上大致看了一下。應該是利用預存程序裡邊操作比較方便
大家有什麼方法能夠分享一下哦

oracle中REGEXP_SUBSTR方法的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.