日期:2014-05-18 浏览次数:20600 次
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 */
------解决方案--------------------
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)
------解决方案--------------------
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) )
------解决方案--------------------
;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)