先写个函数(作用:把学科和分数并起来)<br>create or replace function f_get_bname_score(var_a1 in varchar2) return varchar2 is<br> v_Result varchar2(32767);<br> cursor c_bname is<br> select bname,score<br> from 表名<br> where pname = var_a1;<br>begin<br> for cbname in c_bname loop<br> v_Result := v_Result ||cbname.bname||'~'||cbname.score ||'/';<br> end loop;<br> return(v_Result);<br>end f_get_bname_score;<br>查询(反复利用Oracle instr和substr函数):<br><br>select pname,<br> substr(ChineseScore, 1, instr(ChineseScore, '~') - 1) as Chinese,<br> substr(ChineseScore, instr(ChineseScore, '~') + 1, 20) as ChineseScore,<br> substr(EnglishScore, 1, instr(EnglishScore, '~') - 1) as English,<br> substr(EnglishScore, instr(EnglishScore, '~') + 1, 20) as EnglishScore,<br> substr(MathScore, 1, instr(MathScore, '~') - 1) as Math,<br> substr(MathScore, instr(MathScore, '~') + 1, 20) as MathScore<br> from (select pname,<br> --rtrim(a, '/'),<br> substr(a, 1, instr(a, '/') - 1) as ChineseScore,<br> rtrim(substr(a, - (instr(a, '/'))), '/') as EnglishScore,<br> --substr(a,instr(a, '/')+1),<br> substr(substr(a, instr(a, '/') + 1),<br> 1,<br> instr(substr(a, instr(a, '/') + 1), '/') - 1) as MathScore<br> from (select distinct pname, f_get_bname_score(pname) as a<br> from test_table))<br>缺点:<br> 学科是固定3个的,若多于3个需要修改函数和修改查询语句