-- 建表 插入数据 USE 你自己的数据库; CREATE TABLE Member( MID Char(10) PRIMARY KEY, MName Char(50) ); CREATE TABLE Course( FID Char(10) PRIMARY KEY, FName Char(50) ); CREATE TABLE Score1( SID INT PRIMARY KEY, FID Char(10) FOREIGN KEY REFERENCES Course(FID), MID Char(10) FOREIGN KEY REFERENCES Member(MID), Scores INT ); INSERT INTO Member VALUES ( '1', '张三' ), ( '2', '李四' ), ( '3', '王强' ), ( '4', '张珊' ) INSERT INTO Course VALUES ( '11', '语文' ), ( '22', '数学' ), ( '33', '英语' ), ( '44', '历史' ) INSERT INTO Score1 VALUES ( 101, '11', '1', 99 ), ( 102, '22', '1', 70 ), ( 103, '33', '1', 60 ), ( 104, '44', '1', 80 ), ( 201, '11', '2', 66 ), ( 202, '22', '2', 76 ), ( 203, '33', '2', 97 ), ( 204, '44', '2', 84 ), ( 301, '11', '3', 23 ), ( 302, '22', '3', 43 ), ( 303, '33', '3', 65 ), ( 304, '44', '3', 86 ), ( 401, '11', '4', 54 ), ( 402, '22', '4', 87 ), ( 403, '33', '4', 90 ), ( 404, '44', '4', 31 )
-- 查询各个学生语文、数学、英语、历史课程成绩 ------------------------------------------- -- SELECT m.*, s.Scores FROM Member m -- INNER JOIN Score s ON m.MID = s.MID -- INNER JOIN Course c ON s.FID = c.FID -- WHERE c.FID = (SELECT FID FROM Course -- WHERE FName = '语文') -- 先联表查出语文的成绩及相关内容 -- 在copy其他修改 -------------------------------------------- SELECT mb.MName AS 姓名, tempChinese.Scores AS 语文, tempMath.Scores AS 数学, tempEnlish.Scores AS 英语, tempLong.Scores AS 历史 FROM Member mb LEFT JOIN (SELECT m.*, s.Scores FROM Member m INNER JOIN Score s ON m.MID = s.MID INNER JOIN Course c ON s.FID = c.FID WHERE c.FID = (SELECT FID FROM Course WHERE FName = '语文')) tempChinese ON mb.MID = tempChinese.MID LEFT JOIN (SELECT m.*, s.Scores FROM Member m INNER JOIN Score s ON m.MID = s.MID INNER JOIN Course c ON s.FID = c.FID WHERE c.FID = (SELECT FID FROM Course WHERE FName = '数学')) tempMath ON tempMath.MID = tempChinese.MID LEFT JOIN (SELECT m.*, s.Scores FROM Member m INNER JOIN Score s ON m.MID = s.MID INNER JOIN Course c ON s.FID = c.FID WHERE c.FID = (SELECT FID FROM Course WHERE FName = '英语')) tempEnlish ON mb.MID = tempEnlish.MID LEFT JOIN (SELECT m.*, s.Scores FROM Member m INNER JOIN Score s ON m.MID = s.MID INNER JOIN Course c ON s.FID = c.FID WHERE c.FID = (SELECT FID FROM Course WHERE FName = '历史')) tempLong ON mb.MID = tempLong.MID
-- 虽然多个子表连起来 也可以使用 但是太繁琐了 -- 通过姓名分组 max找最大值 利用case 就可以很方便了 SELECT m.MName as 姓名, max(case c.FName when '语文' then s.Scores else 0 end) as 语文, max(case c.FName when '数学' then s.Scores else 0 end) as 数学, max(case c.FName when '英语' then s.Scores else 0 end) as 英语, max(case c.FName when '历史' then s.Scores else 0 end) as 历史 From Score1 as s INNER JOIN Member m ON m.MID=s.MID inner join Course as c on c.FID=s.FID GROUP BY m.MName
-- 利用存储过程 分数小于70 create PROCEDURE #scoreinit @score int AS SELECT m.MName as 姓名, max(case c.FName when '语文' then s.Scores else 0 end) as 语文, max(case c.FName when '数学' then s.Scores else 0 end) as 数学, max(case c.FName when '英语' then s.Scores else 0 end) as 英语, max(case c.FName when '历史' then s.Scores else 0 end) as 历史 From Score1 as s INNER JOIN Member m ON m.MID=s.MID inner join Course as c on c.FID=s.FID WHERE s.Scores < @score GROUP BY m.MName -- drop PROCEDURE #scoreinit EXEC #scoreinit @score=70