select
max(s.[name]) AS 姓名,
max(case when sc.cid='1' then sc.score END) as English,
max(case when sc.cid='2' then sc.score END) as Chinese,
max(case when sc.cid='3' then sc.score END) as History
--...当然这里可以有多少个的话以此类推,但是sc.cid 的值一定要和C表中的课程ID一致,且别名也要和课程名称一致
from sc,s where sc.sid = s.id group by sc.sid
GO
DECLARE @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when sc.cid='''+Convert(varchar,sc.CID)+''' then sc.score end) ['+Convert(varchar,c.[name])+']'
from sc,c WHERE sc.CID = c.id group by sc.cid,c.[name]
set @sql=stuff(@sql,1,1,'')
PRINT @sql
exec ('select max(s.[name]) as [Name],'+@sql+' from sc,s where sc.sid = s.id group by sc.sid')