如果我明白你的需要,这可能是一种方法。
说你有一个像这样的表:
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