如果我明白你的需要,這可能是一種方法。
說你有一個像這樣的表:
create table yourTable(setid, codes, messagedescr) as (
select 1, 'A, B, C, D', 'You can login' from dual union all
select 2, 'B, C, D' , 'You can login for one day' from dual union all
select 3, 'A, C, E' , 'You can login but update your profile' from dual union all
select 4, 'B, C, E, F', 'You cannot login' from dual
).
這可能是一個辦法:
with inputData(codes) as (
select listagg(trim (regexp_substr(input_codes, '[^,]+', 1, level))) within group (order by trim (regexp_substr(input_codes, '[^,]+', 1, level)))
from (select 'A, D, C, B' as input_codes from dual) /* the input string */
CONNECT BY instr(input_codes, ',', 1, level - 1) > 0
)
select *
from inputData
inner join (
select listagg(trim (regexp_substr(codes, '[^,]+', 1, level)))
within group (order by trim (regexp_substr(codes, '[^,]+', 1, level))) as codes,
messagedescr
from yourTable
CONNECT BY instr(codes, ',', 1, level - 1) > 0
and prior setId = setId
and prior sys_guid() is not null
group by setId, messagedescr
)
using (codes)
這裏的想法是分裂在許多行你輸入的字符串,然後聚集所產生的按字母順序排列,然後對錶中的值應用相同的順序,然後檢查排序的字符串是否相等。
這部分是用於分割,順序和聚合的輸入值,以使得結果是有序字符串:
select listagg(trim (regexp_substr(input_codes, '[^,]+', 1, level))) within group (order by trim (regexp_substr(input_codes, '[^,]+', 1, level)))
from (select 'A, D, C, B' as input_codes from dual) /* the input string */
CONNECT BY instr(input_codes, ',', 1, level - 1) > 0
給出:
ABCD
這部分是用來做同你的桌子上:
select listagg(trim (regexp_substr(codes, '[^,]+', 1, level)))
within group (order by trim (regexp_substr(codes, '[^,]+', 1, level))) as codes,
messagedescr
from yourTable
CONNECT BY instr(codes, ',', 1, level - 1) > 0
and prior setId = setId
and prior sys_guid() is not null
group by setId, messagedescr
給出:
CODES MESSAGEDESCR
---------- -------------------------------------
ABCD You can login
BCD You can login for one day
ACE You can login but update your profile
BCEF You cannot login
這些部分結果之間的連接非常簡單,只需檢查您的表中是否存在與(有序)輸入字符串對應的值(有序)。
能否請您發佈一些示例數據和預期的結果? (格式文本,請) – Aleksej
@Tsiftelis Thanasis,如果您發佈A,B,C表示whict消息,你想dispaly.ie A的消息或B的消息或C的消息,什麼是你優先級。 – Mansoor
這是DB設計不佳的後果。不要將多個代碼存儲在單個列中;使用單獨的表格。 – dasblinkenlight