我正面临一个奇怪的场景,我需要匹配一个包含varchar的整数,其中包含用oracle中逗号分隔的数字
示例:
表t1:
id integer key integer
表t2
id integer, keys varchar2
T1值为:
1,111 2,201 3,301
T2值为:
1, "111,301" 2, "111,201" 3, "201,301"
问题:有没有办法可以匹配,或者regular_expression与T1的密钥匹配T2的密钥?
答案 0 :(得分:1)
你可以在没有正则表达式的情况下进行常规连接:
select *
from t1
inner join t2
on ','||t2.keys||',' like '%,'||to_char(t1.key)||',%';
例如:
SQL> create table t1(id, key)
2 as
3 select 1, 111 from dual union all
4 select 2, 201 from dual union all
5 select 3, 301 from dual;
Table created.
SQL> create table t2(id, keys)
2 as
3 select 1, '111,301' from dual union all
4 select 2, '111,201' from dual union all
5 select 3, '201,301' from dual;
Table created.
SQL> select *
2 from t1
3 inner join t2
4 on ','||t2.keys||',' like '%,'||to_char(t1.key)||',%';
ID KEY ID KEYS
---------- ---------- ---------- -------
1 111 1 111,301
1 111 2 111,201
2 201 2 111,201
2 201 3 201,301
3 301 1 111,301
3 301 3 201,301
6 rows selected.
这不是正则表达式,只是连接。例如,假设我们想比较
KEY KEYS
111 111,301
我们可以说
where keys like '%'||key||'%'
即。扩展,这是
where '111,301' like '%111%'
匹配罚款。但我也在那里添加了一些逗号。即我这样做了:
where ',111,301,' like '%,111,%'
为什么呢?想象一下,你有这个数据:
KEY KEYS
111 1111,301
如果我们做了简单的连接:
where '1111,301' like '%111%'
它会错误地匹配。通过在双方注入前导和尾随逗号:
where ',1111,301,' like '%,111,%'
不再是错误匹配,因为,1111,
与,111,
不同。