有两张表PlayInfo表,和StudentInfo表
PlayInfo表的结构是
Studentid Play
1001,1002,1003 数学
1001,1004 语文
1002,1003 外语
StudentInfo表结构如下
Studentid Studentname
1001 小明
1002 小刚
1003 小涛
1004 小波
现在要查询出的结果为
Studentname Play
小明,小刚,小涛 数学
小明,小波 语文
小刚,小涛 外语
请问怎么实现啊??
PlayInfo表的结构是
Studentid Play
1001,1002,1003 数学
1001,1004 语文
1002,1003 外语
StudentInfo表结构如下
Studentid Studentname
1001 小明
1002 小刚
1003 小涛
1004 小波
现在要查询出的结果为
Studentname Play
小明,小刚,小涛 数学
小明,小波 语文
小刚,小涛 外语
请问怎么实现啊??
CREATE OR REPLACE FUNCTION sf_get_studentname(instr IN VARCHAR2)
RETURN VARCHAR2
IS
outstr varchar2(4000);
BEGIN EXECUTE IMMEDIATE 'select WMSYS.WM_CONCAT(Studentname
)
from (
select Studentname from StudentInfo where Studentid in ('||instr||')
order by Studentid
)'
INTO outstr;
return outstr
EXCEPTION
WHEN NO_DATA_FOUND
THEN
retrun null;
WHEN OTHERS
THEN
return null;
END sf_get_studentname;
/
以上是10G 的写法
根据mantisXF以前一个帖子模仿而来的。
*/create table PlayInfo(Studentid varchar2(20),Play varchar2(20));
insert into PlayInfo values('1001,1002,1003', '数学');
insert into PlayInfo values('1001,1004', '语文');
insert into PlayInfo values('1002,1003', '外语');create table StudentInfo(Studentid varchar2(20),Studentname varchar2(20));
insert into StudentInfo values('1001', '小明');
insert into StudentInfo values('1002', '小刚');
insert into StudentInfo values('1003', '小涛');
insert into StudentInfo values('1004', '小波');
commit;select wmsys.wm_concat(t1.Studentname) Studentname, t2.Play
from StudentInfo t1,
(select substr(',' || Studentid || ',',
instr(',' || Studentid || ',', ',', 1, rn) + 1,
instr(',' || Studentid || ',', ',', 1, rn + 1) -
instr(',' || Studentid || ',', ',', 1, rn) - 1) as new_Studentid,
Play
from PlayInfo,
(select rownum rn
from all_objects
where rownum <= (select max(length(Studentid) -
length(replace(Studentid, ',')) + 1)
from PlayInfo))) t2
-- where instr(',' || Studentid, ',', 1, rn) > 0) t2
where t1.studentid = t2.new_Studentid
group by t2.Play输出:
/* 输出: (保留问题,PLAY没能是之前的顺序)STUDENTNAME PLAY
------------------------- --------------------
小明,小刚,小涛 数学
小刚,小涛 外语
小明,小波 语文
*/