# 多对对关系查询

0

 Stu_id Stu_name 1 张三 2 李四 3 王五

 Co_id Co_name 1 Java 2 C# 3 C 4 C++ 5 Javascript

 Stu_id Co_id 1 1 1 4 2 1 2 3 2 5

 张三 Java 张三 C++

0

select Stu_name,stuff(select ',' + Co_name

from 课程表

where Co_id=g.Co_id for xml path(''),1,1'') as 课程

from 学生表 as x inner join  关系表 as g on x.Stu_id=g.Stu_id

Quentin lee | 菜鸟二级 |园豆：230 | 2010-12-01 17:55

0

0

0

select stu_id,co_id,(select convert(varchar(20),co_id)+',' from t3 where a.stu_id=stu_id for xml path('')) from t3 a 嘎嘎，这是我试验的 for xml path('')，逻辑太丑了，至少得到 stu_id 1(1,4,) stu_id 2(1,3,5,)

select distinct stu_id,(select convert(varchar(20),co_id)+',' from t3 where a.stu_id=stu_id for xml path('')) from t3 a select distinct stu_name,(select convert(varchar(20),co_id)+',' from t3 where a.stu_id=stu_id for xml path('')) from t3 a,t1 b where a.stu_id=b.stu_id select distinct stu_name,stuff((select ','+co_name from t3,t2 where a.stu_id=stu_id and t3.co_id=t2.co_id for xml path('')),1,1,'') from t3 a,t1 b where a.stu_id=b.stu_id

0

select b.stu_name,c.co_name
from
relation a
left join student b on a.stu_id=b.stu_id
left join course c on a.co_id=c.co_id
where b.stu_name='张三'

0

SELECT     dbo.Stu.Stu_name, dbo.Co.Co_nameFROM         dbo.Co INNER JOIN                      dbo.T3 ON dbo.Co.Co_id = dbo.T3.Co_id INNER JOIN                      dbo.Stu ON dbo.T3.Stu_id = dbo.Stu.Stu_idWHERE     (dbo.Stu.Stu_name = '张三')

yuqicook | 园豆：205 (菜鸟二级) | 2010-12-02 22:14
0

feichexia | 园豆：205 (菜鸟二级) | 2010-12-10 19:28

您需要登录以后才能回答，未注册用户请先注册