select ci_id, wm_concat(name)
from (select ci_id, decode(instr(stu_ids, stu_id), 0, '', stu_name) name
from (select c.ci_id, c.stu_ids, s.stu_id, s.stu_name
from pm_ci c, pm_stu s))
group by ci_id;
from (select ci_id, decode(instr(stu_ids, stu_id), 0, '', stu_name) name
from (select c.ci_id, c.stu_ids, s.stu_id, s.stu_name
from pm_ci c, pm_stu s))
group by ci_id;
6分钟这位置解释rownum,被强行带偏了。在我11g里,rownum始终都是伪列。参考以下两个sql
select rownum,sal from emp order by sal desc;
select rownum,sal from(
select rownum,sal from emp order by sal desc
);
两个sql执行的结果是一样的,除了rownum列。
第一条sql的rownum是乱序的,第二条是有序的。但其他列顺序是一样的。倘若第二条sql中的子查询的rownum在主查询中真的变成了实列,那么两个查询中的rownum列应该相同。
select rownum,sal from emp order by sal desc;
select rownum,sal from(
select rownum,sal from emp order by sal desc
);
两个sql执行的结果是一样的,除了rownum列。
第一条sql的rownum是乱序的,第二条是有序的。但其他列顺序是一样的。倘若第二条sql中的子查询的rownum在主查询中真的变成了实列,那么两个查询中的rownum列应该相同。
-- 设置列宽
col ci_id format a20
col stu_names format a20
--行显示设置
set linesize 100
-- sql查询语句
select pc.ci_id ci_id,wm_concat(pt.stu_name) stu_names
from pm_ci pc,pm_stu pt
where instr(pc.stu_ids,pt.stu_id)>0
group by pc.ci_id;
col ci_id format a20
col stu_names format a20
--行显示设置
set linesize 100
-- sql查询语句
select pc.ci_id ci_id,wm_concat(pt.stu_name) stu_names
from pm_ci pc,pm_stu pt
where instr(pc.stu_ids,pt.stu_id)>0
group by pc.ci_id;
select *
from (select rownum rm,e1.* from (select * from emp order by sal desc) e1 where rownum<=8) e2
where rm>=5;
from (select rownum rm,e1.* from (select * from emp order by sal desc) e1 where rownum<=8) e2
where rm>=5;
刚开始有点不理解,觉得这种求解应该先通过分组函数和avg函数求出每个部门的平均值,再多表查询设定条件合并两张表。现在学习了新方法!省力多了!
2020-02-21