日期:2014-05-17 浏览次数:20791 次
with A as
(
select '111,222,333' userId,'aaa,bbb,ccc' userName from dual
),B as
(
select '111,222' userId ,'1,1' userState from dual
)
select t1.userId,t1.userName,nvl(t2.userState,0) userState
from
(
select replace(regexp_substr(userId,'[^,]+',1,level),',',' ') userId,
replace(regexp_substr(userName,'[^,]+',1,level),',',' ') userName
from A
connect by level<=length(userId)-length(replace(userId,',',''))+1
) t1 left join
(
select replace(regexp_substr(userId,'[^,]+',1,level),',',' ') userId,
replace(regexp_substr(userState,'[^,]+',1,level),',',' ') userState
from B
connect by level<=length(userId)-length(replace(userId,',',''))+1
) t2 on t1.userId = t2.userId
userId userName userState
----------------------------------
1 111 aaa 1
2 222 bbb 1
3 333 ccc 0
with A as
(
select '111,222,333' userId,'aaa,bbb,ccc' userName from dual union all
select '444,555' userId,'ddd,eee' userName from dual union all
select '666' userId,'fff' userName from dual
),B as
(
select '111,222' userId ,'1,1' userState from dual union all
select '444,666' userId ,'1,1' userState from dual
)
select t1.userId,t1.userName,nvl(t2.userState,0) userState
from
(
select distinct replace(regexp_substr(userId,'[^,]+',1,level),',',' ') userId,
replace(regexp_substr(userName,'[^,]+',1,level),',',' ') userName
from A
connect by level<=length(userId)-length(replace(userId,',',''))+1
) t1 left join
(
select distinct replace(regexp_substr(userId,'[^,]+',1,level),',',' ') userId,
replace(regexp_substr(userState,'[^,]+',1,level),',',' ') userState
from B
connect by level<=length(userId)-length(replace(userId,',',''))+1
) t2 on t1.userId = t2.userId
order by t1.userId
userId userName userState
----------------------------------
1 111 aaa 1
2 222 bbb 1
3 333 ccc 0
4 444 ddd 1