日期:2014-05-18  浏览次数:20600 次

求SQL语句,面试是没有做出来
查询学号==1的同学学习的课程完全相同的其他同学学号和姓名

表结构如下:

Student(sid,Sname) 学生表
Course(cid,Cname) 课程表
SC(sid,cid,score) 成绩表

------解决方案--------------------
go
if OBJECT_ID('student')is not null
drop table tbl
go
create table student(
stuid int,
stuname varchar(10)
)
go
insert student
select 1,'tracy' union all
select 2,'tom' union all
select 3,'lucy' union all
select 4,'cate' union all
select 5,'lily'
go
if OBJECT_ID('course') is not null
drop table course
go
create table Course(
cid int,
cname varchar(10)
)
go
insert course
select 1,'javaee' union all
select 2,'c++' union all
select 3,'java' union all
select 4,'jsp' union all
select 5,'SQL Server'

go
if OBJECT_ID('SC') is not null
drop table SC
go
create table SC(
stuid int,
cid int,
score int
)
go
insert SC
select 1,2,68 union all
select 1,3,68 union all
select 1,5,68 union all
select 2,2,68 union all
select 2,4,68 union all
select 3,2,68 union all
select 3,3,68 union all
select 3,5,68 union all
select 4,1,68 union all
select 5,1,68 union all
select 5,2,68 

select a.stuid,b.stuname from(
select stuid from SC where cid in(select cid from SC
where stuid=1) and stuid<>1
group by stuid having COUNT(*)=(select 
COUNT(cid) from SC where stuid=1))a
inner join student b on a.stuid=b.stuid
/*
stuid stuname
3 lucy
*/
------解决方案--------------------
SQL code

select a.stuid,b.stuname from(
select stuid from SC where 
exists( select 1 from SC where stuid=1) 
and stuid<>1
group by stuid 
having COUNT(*)=(select 
COUNT(cid) from SC where stuid=1))a
inner join student b on a.stuid=b.stuid
/*
stuid stuname
3 lucy
*/

------解决方案--------------------
SQL code

declare @sql nvarchar(max)
set @sql='
;with t as(
select stuid,stuname,'
select  @sql=@sql+' max(case cname when '''+isnull(cname,'')+''' then ''1'' else ''0'' end)+' from (select distinct cname from student inner join SC
on student.stuid=SC.stuid inner join course
on course.cid=SC.cid)t
select @sql=substring(@sql,1,len(@sql)-1)
select @sql=@sql+'as course  from (select student.stuid,student.stuname,cname from student inner join SC
on student.stuid=SC.stuid inner join course
on course.cid=SC.cid) t
group by stuid,stuname)
select t1.stuname from t left join t t1 on t.course=t1.course
and t.stuname<>t1.stuname
where t.stuid=1
'
exec( @sql)

------解决方案--------------------
SQL code

select t.sid,t.sname
from student t
where t.sid<>1 and 
not exists(
(select b.cid from student a 
 inner join sc b on a.sid=b.sid and a.sid=1)
 except
(select b.cid from student a
 inner join sc b on a.sid=b.sid and a.sid=t.sid)
)

------解决方案--------------------
探讨
SQL code

select t.sid,t.sname
from student t
where t.sid<>1 and
not exists(
(select b.cid from student a
inner join sc b on a.sid=b.sid and a.sid=1)
except
(select b.cid from student a
……

------解决方案--------------------
SQL code

;with t as (
select student.stuid,student.stuname,SC.cid,b.CNT from student inner join SC
on student.stuid=SC.stuid inner join (select stuid,count(*) as CNT from SC group by stuid) b
on b.stuid=student.stuid)
select t1.stuid,t1.stuname from t left join t t1
on t.cid=t1.cid
and t.stuid<>t1.stuid
where t.stuid=1
and t1.CNT=t.CNT
group by t1.stuid,t1.stuname
having count(1)=avg(t.CNT)