一条SQL语句,关于字符分割关联多条记录的问题
原文传送门:http://topic.csdn.net/u/20091010/14/FC7737C1-D60B-43F1-A8B5-A9EEF2DE4426.html
假如现在有两张表:
1.表stuinfo
sid sname subs
1 jack |1|2|
2 marry |1|4|
3 tom |3|
2.表subinfo
subid subname
1 physics
2 maths
3 biology
4 geography
我想把stuinfo的subs字段按|字符分开,然后匹配subinfo的subid,取subname的值,匹配到多个的话就用逗号隔开。
简而言之我想得到的结果如下:
sid sname subname
1 jack physics,maths
2 marry physics,geography
3 tom biology
希望各位大侠能帮忙设计一下SQL语句,谢谢了!
Oracle 10g以上版本
select a.sid,a.sname,wm_concat(b.subname) subname
from stuinfo a,subinfo b
where instr(a.subs,'|'||b.subid(+)||'|')>0
group by a.sid,a.sname
9i的
select sid,sname,substr(max(sys_connect_by_path(subname,',')),2) subname
from (
select a.sid,a.sname,b.subname,
row_number()over(partition by a.sid order by rownum)rn
from stuinfo a,subinfo b
where instr(a.subs,'|'||b.subid(+)||'|')>0)
connect by prior rn=rn-1 and prior sid=sid
start with rn=1
group by sid,sname
相关文档:
表:TABLEA
客户编号 应收金额 收款金额
1001 100 80
1001 200 180&nb ......
ORACLEPL/SQL基础--游标的学习来源:
游标字面理解就是游动的光标。
用数据库语言来描述:游标是映射在结果集中一行数据上的位置实体,有了游标用户就可以访问结果集中的任意一行数据了,将游标放置到某行后,即可对该行数据进行操作,例如提取当前行的数据等等。
......
1:应用程序不再需要使用 Class.forName() 显式地加载 JDBC 驱动程序。当前使用 Class.forName() 加载 JDBC 驱动程序的现有程序将在不作修改的情况下继续工作。
2:需要注意以下命令:
executeUpdate:是最基础的数据库的更新、插入和删除操作。效率低下。
executeQuery:是最基础的执行查询语句,同样 ......
数据库sql语句系列都是以下面的表为基础的
1. 写出步骤4中的各项操作的SQL语句。
① 给学生表增加一属性Nation(民族),数据类型为Varchar(20);
alter table Student add Nation varchar(20);
② 删除学生表中新增的属性Nation;
alter table Student DR ......
declare @id varchar(10)
declare @nm varchar(10)
declare @table as table(emp varchar(10),empname varchar(10))
declare CurEmp cursor for select top 6 empid,empname from employee
open CurEmp
fetch next from CurEmp into @id,@nm
While @@fetch_status=0
begin
insert @table(emp,e ......