解决方案是规范化您的表格。其他所有解决方案都是复杂且低效的。
正确的标准方法是使用分层查询或流水线表函数将值拆分为列(即规范化它们),然后以这种方式进行比较,但如果您有有限数量的管道,那么您应该能够使用标准的SUBSTR() 和INSTR() 函数在管道上拆分并进行比较。
将管道连接到要搜索的字符串的末尾,然后按照以下方式将其拆分,然后在第二个字符串中搜索您要查找的字符串:
with the_string as (
select '|' || '12|14|15' || '|' as str1
, '|' || '12|15|14' || '|' as str2
from dual
)
select substr(str1, instr(str1, '|', 1, 1), instr(str1, '|', 1, 2))
, substr(str1, instr(str1, '|', 1, 2), instr(str1, '|', 1, 3) - instr(str1, '|', 1, 2) + 1)
, substr(str1, instr(str1, '|', 1, 3), instr(str1, '|', 1, 4) - instr(str1, '|', 1, 3) + 1)
from the_string
where str2 like '%' || substr(str1, instr(str1, '|', 1, 1), instr(str1, '|', 1, 2)) || '%'
and str2 like '%' || substr(str1, instr(str1, '|', 1, 2), instr(str1, '|', 1, 3) - instr(str1, '|', 1, 2) + 1) || '%'
and str2 like '%' || substr(str1, instr(str1, '|', 1, 3), instr(str1, '|', 1, 4) - instr(str1, '|', 1, 3) + 1) || '%'
当字符串长度相同但内容不同时,不会返回任何内容,因为内容不同,当字符串长度不同时,不会返回任何内容,因为返回的“值”之一将为 NULL,您不能直接比较空值。
SQL Fiddle
正如我所说,标准化您的数据库虽然...简单得多。