日期:2014-05-17  浏览次数:20730 次

关于ORACLE SPLIT 查询的问题,望大虾解答
有两个表的关系是这样的。
table1:
id iid
1 A,B
2 B,C,D
3 A,D

TABLE2:
ID MC
A this is a
B this is B
C this is B


我想通过tbale1的ID做为参数传值,查询TABLE2包含在TBALE1.iid中的所有记录。
现在我已有split函数来拆分IID,请问要实现我上述的需求要怎么写呢? 


------解决方案--------------------
SQL code


with table1(id,iid) as(
select 1,'A,B' from dual
union all select 2,'B,C,D' from dual
union all select 3,'A,D' from dual
),
table2(id,mc) as(
select 'A','this is a' from dual
union all select 'B','this is b' from dual
union all select 'C','this is c' from dual
)
select * from table2
where exists(
select 1 from table1 where table1.id=1--此处传你的id
 and instr('%,'||iid||',%',','||table2.id||',')>0);
/*
I MC                                                                            
- ---------                                                                     
A this is a                                                                     
B this is b  
*/