请用sql回答我,谢谢大家了
create table student
(
stuid char(3) primary key,
stuname varchar(20)not null,
stusex varchar(5)not null,
stuclass varchar(20)not null,
stuage smallint,
)
create table kecheng
(
kcid char(3)primary key,
kcname varchar(50),
kcteacher varchar(20)
)
create table xuankebiao
(
stuid char(3),
kcid char(3)
)
insert into student values('001','zhangsan','man','001',20)
insert into student values('002','lisi','woman','001',24)
insert into student values('003','wangwu','man','001',22)
insert into student values('004','zhouliu','woman','001',18)
insert into kecheng values('001','语文','aa')
insert into kecheng values('002','数学','bb')
insert into kecheng values('003','英语','cc')
insert into kecheng values('004','逻辑','dd')
insert into kecheng values('005','生物','ee')
insert into xuankebiao values('001','001')
insert into xuankebiao values('001','002')
insert into xuankebiao values('001','003')
insert into xuankebiao values('001','004')
求各位大哥大姐的帮忙解决一下

解决方案 »

  1.   

    if object_id('student') is not null drop table student
    go
    create table student
    (
    stuid char(3) primary key,
    stuname varchar(20)not null,
    stusex varchar(5)not null,
    stuclass varchar(20)not null,
    stuage smallint,
    )
    if object_id('kecheng') is not null drop table kecheng
    go
    create table kecheng
    (
    kcid char(3)primary key,
    kcname varchar(50),
    kcteacher varchar(20)
    )
    if object_id('xuankebiao') is not null drop table xuankebiao
    go
    create table xuankebiao
    (
    stuid char(3),
    kcid char(3)
    )
    insert into student values('001','zhangsan','man','001',20)
    insert into student values('002','lisi','woman','001',24)
    insert into student values('003','wangwu','man','001',22)
    insert into student values('004','zhouliu','woman','001',18)
    insert into kecheng values('001','语文','aa')
    insert into kecheng values('002','数学','bb')
    insert into kecheng values('003','英语','cc')
    insert into kecheng values('004','逻辑','dd')
    insert into kecheng values('005','生物','ee')
    insert into xuankebiao values('001','001')
    insert into xuankebiao values('001','002')
    insert into xuankebiao values('001','003')
    insert into xuankebiao values('001','004')
    insert into xuankebiao values('002','001')
    insert into xuankebiao values('002','002')
    insert into xuankebiao values('003','003')
    insert into xuankebiao values('003','004');with cte as(
    select distinct s.*,k.*
    from student s join xuankebiao x on s.stuid=x.stuid
    join kecheng k on x.kcid=k.kcid
    )
    select distinct stuid,stuname
    ,kcname=stuff((select ','+kcname from cte where stuid=t.stuid for xml path('')),1,1,'')
    from cte t/*
    stuid stuname kcname
    ------------------------------------------
    001 zhangsan 语文,数学,英语,逻辑
    002 lisi 语文,数学
    003 wangwu 英语,逻辑
    */
      

  2.   


    use tempdb;
    /*
    create table student
    (
    stuid char(3) primary key,
    stuname varchar(20)not null,
    stusex varchar(5)not null,
    stuclass varchar(20)not null,
    stuage smallint,
    )
    create table kecheng
    (
    kcid char(3)primary key,
    kcname varchar(50),
    kcteacher varchar(20)
    )
    create table xuankebiao
    (
    stuid char(3),
    kcid char(3)
    )
    insert into student values('001','zhangsan','man','001',20)
    insert into student values('002','lisi','woman','001',24)
    insert into student values('003','wangwu','man','001',22)
    insert into student values('004','zhouliu','woman','001',18)
    insert into kecheng values('001','语文','aa')
    insert into kecheng values('002','数学','bb')
    insert into kecheng values('003','英语','cc')
    insert into kecheng values('004','逻辑','dd')
    insert into kecheng values('005','生物','ee')
    insert into xuankebiao values('001','001')
    insert into xuankebiao values('001','002')
    insert into xuankebiao values('001','003')
    insert into xuankebiao values('001','004')
    insert into xuankebiao values('002','001')
    insert into xuankebiao values('002','002')
    insert into xuankebiao values('003','003')
    insert into xuankebiao values('003','004')
    */
    select distinct t1.stuid,t1.stuname,
    kcname=stuff((select ','+kcname from 
    (
    select s.stuid,s.stuname,k.kcname
    from kecheng as k
    join xuankebiao as x on x.kcid = k.kcid
    join student as s on s.stuid = x.stuid
    )
    as t2 where stuid=t1.stuid for xml path('')),1,1,'')
    from
    (
    select s.stuid,s.stuname,k.kcname
    from kecheng as k
    join xuankebiao as x on x.kcid = k.kcid
    join student as s on s.stuid = x.stuid
    ) as t1;