一条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
相关文档:
第一部分
单表查询
例一:查询全体学生的学号与姓名
SELECT Sno,Sname
from Student;
例二:查询全体学生的姓名、学号、所在系
SELECT Sname,Sno,Sdept
from Student;
例三:查询全体学生的详细记录
SELECT *
from Student;
等价于:
SELECT *
from Student;
例四:查询全体学生的姓名及其出生年份
......
1. 如果你希望使用selcet top语句,并且还要附带where条件,那么条件中的列就得是合适的索引,如聚集索引、复合索引里的主列
等,同时,where条件里也要尽量避开使用函数,or,判断NULL等会引起全部扫描的语句,不然执行的是全表扫描。
2. 通过设置STATISTICS我们可以查看执行SQL时的执行效率以及相关性能测试 ......
select datediff(month,'2009-11-02','2009-12-01')
不知道你要怎么个月差异
上面的命令结果也是1
但是其实按照常识差异不是一个月,是差一天一个月
这有段现成的代码可以帮助你算出常识上的差异
SQL code
declare @t table(a datetime,b datetime);
insert @t
select '2009-11-02','2009-12-01' UNION ......
最近为数据库服务器增加了内存,达到了最大支持的8G,数据库用的是mssql 2005 ,之前内存一直是4G的,不存在内存大和32位操作系统冲突的事情,32位操作系统单进程最大支持2G的内存,这样子的话内存就白加了,怎么办呢?
网上搜索了很多资料,发现微软提供了一个算是临时的解决方案吧,使用AWE来分配内存,这样子sqlse ......