MySQL(學生表、教師表、課程表、成績表)多表查詢
阿新 • • 發佈:2019-01-31
多表查詢SQL語句
1、表架構
student(sid,sname,sage,ssex) 學生表
course(cid,cname,tid) 課程表
sC(sid,cid,score) 成績表
teacher(tid,tname) 教師表
2、建表sql語句
CREATE TABLE student ( sid INT PRIMARY KEY NOT NULL, sname VARCHAR(30), sage INT, ssex VARCHAR(8) ) CREATE TABLE course ( cid INT PRIMARY KEY NOT NULL, cname VARCHAR(30), tid INT ) CREATE TABLE sc ( sid INT NOT NULL, cid INT NOT NULL, score INT ) CREATE TABLE teacher ( tid INT PRIMARY KEY NOT NULL, tname VARCHAR(30) )
3、問題:
(1)查詢“30001”課程的所有學生的學號與分數;
SELECT sid,score FROM sc WHERE cid="30001"
(2)查詢“001”課程比“002”課程成績高的所有學生的學號與分數;
SELECT a.sid,a.score FROM (SELECT sid,score FROM sc WHERE cid="30001") a,
(SELECT sid,score FROM sc WHERE cid="30002") b
WHERE a.score>b.score AND a.sid=b.sid
(3)查詢平均成績大於60分的同學的學號和平均成績;
SELECT sid,AVG(score)
FROM sc
GROUP BY sid HAVING AVG(score)>60
(4)查詢所有同學的學號、姓名、選課數、總成績
SELECT s.sid AS "學號", s.sname AS "姓名", COUNT(sc.cid) AS "課程數目", SUM(sc.score) AS "總分數"
FROM student s, sc sc
WHERE s.sid=sc.sid
GROUP BY s.sid
(5)查詢姓“李”的老師的個數;
select count(distinct(Tname)) from teacher where tname like '李%';
(6)查詢學過“張三”老師課的同學的學號、姓名
SELECT s.sid AS "學號", s.sname AS "姓名"
FROM student s, sc sc, course c, teacher t
WHERE s.sid=sc.sid AND c.cid=sc.cid AND t.tid=c.tid AND t.tname="張三"
(7)查詢沒有學過“張三”老師課的同學的學號、姓名
SELECT s.sid, s.sname
FROM student s
WHERE s.sid NOT IN (
SELECT s.sid
FROM student s, sc sc, course c, teacher t
WHERE s.sid=sc.sid AND c.cid=sc.cid AND t.tid=c.tid AND t.tname="張三"
)
(8)查詢學過“30001”並且也學過編號“30002”課程的同學的學號、姓名
SELECT s.sid, s.sname
FROM student s, sc sc
WHERE s.sid=sc.sid AND sc.cid="30001" AND EXISTS(
SELECT * FROM sc AS sc2 WHERE sc2.sid=sc.sid AND sc2.cid="30002"
)
(9)查詢學過“葉平”老師所教的所有課的同學的學號、姓名;
SELECT sid, sname
FROM student
WHERE sid IN (
SELECT sc.sid
FROM sc sc, course c, teacher t
WHERE sc.cid=c.cid AND c.tid=t.tid AND t.tname="張二"
)
(10)查詢所有課程成績小於60分的同學的學號、姓名
SELECT sid, sname FROM student
WHERE sid NOT IN (
SELECT DISTINCT(sc.sid) FROM student s, sc sc
WHERE sc.sid=s.sid AND sc.score>60)
(11)查詢沒有學全所有課的同學的學號、姓名;
SELECT sid, sname FROM student
WHERE sid NOT IN(
SELECT s.sid FROM student s, sc sc
WHERE sc.sid=s.sid
GROUP BY s.sid
HAVING COUNT(sc.cid)=(
SELECT COUNT(cid) FROM course))
(12)查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分
SELECT cid AS "課程ID", MAX(score) AS "最高分", MIN(score) AS "最低分"
FROM sc
GROUP BY cid
(13)按各科平均成績從低到高和及格率的百分數從高到低順序
(方式一)
SELECT sc.cid AS "課程ID",c.cname AS "課程名", AVG(sc.score) AS "平均成績",
SUM(CASE WHEN sc.score > 60 THEN 1 ELSE 0 END)/COUNT(1)*100 AS "及格百分數"
FROM sc sc, course c
WHERE sc.cid=c.cid
GROUP BY sc.cid
ORDER BY AVG(sc.score) ASC,
SUM(CASE WHEN sc.score > 60 THEN 1 ELSE 0 END)/COUNT(1)*100 DESC
(方式二)
SELECT sc.cid AS "課程ID",c.cname AS "課程名", IFNULL(AVG(sc.score),0) AS "平均成績",
100*SUM(CASE WHEN IFNULL(sc.score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS "及格百分數"
FROM sc sc, course c
WHERE sc.cid = c.cid
GROUP BY sc.cid
ORDER BY AVG(sc.score) ASC,
100*SUM(CASE WHEN IFNULL(sc.score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC
(14)查詢所有學生的所有科目的成績單(學號、姓名、語文、數學、英語、物理、平均分、總分(按照總分由高到低排序))
SELECT s.sid AS "學號", s.sname AS "姓名",
SUM(CASE c.cname WHEN "語文" THEN sc.score ELSE 0 END) AS "語文",
SUM(CASE c.cname WHEN "數學" THEN sc.score ELSE 0 END) AS "數學",
SUM(CASE c.cname WHEN "英語" THEN sc.score ELSE 0 END) AS "英語",
SUM(CASE c.cname WHEN "物理" THEN sc.score ELSE 0 END) AS "物理",
IFNULL(AVG(sc.score),0) AS "平均分",
IFNULL(SUM(sc.score),0) AS "總分"
FROM student s
LEFT OUTER JOIN sc sc ON s.sid=sc.sid
LEFT OUTER JOIN course c ON sc.cid=c.cid
GROUP BY s.sid, s.sname
ORDER BY IFNULL(SUM(sc.score),0) DESC
(15)查詢總分排名在200-300(包含200和第300)之間的學生所有成績單資訊
SELECT s.sid AS "學號", s.sname AS "姓名",
SUM(CASE c.cname WHEN "語文" THEN sc.score ELSE 0 END) AS "語文",
SUM(CASE c.cname WHEN "數學" THEN sc.score ELSE 0 END) AS "數學",
SUM(CASE c.cname WHEN "英語" THEN sc.score ELSE 0 END) AS "英語",
SUM(CASE c.cname WHEN "物理" THEN sc.score ELSE 0 END) AS "物理",
IFNULL(AVG(sc.score),0) AS "平均分",
IFNULL(SUM(sc.score),0) AS "總分"
FROM student s
LEFT OUTER JOIN sc sc ON s.sid=sc.sid
LEFT OUTER JOIN course c ON sc.cid=c.cid
GROUP BY s.sid, s.sname
HAVING IFNULL(SUM(sc.score),0) BETWEEN 200 AND 300
ORDER BY IFNULL(SUM(sc.score),0) DESC
(16)查詢總分排名在前四名的學生所有成績單資訊
SELECT s.sid AS "學號", s.sname AS "姓名",
SUM(CASE c.cname WHEN "語文" THEN sc.score ELSE 0 END) AS "語文",
SUM(CASE c.cname WHEN "數學" THEN sc.score ELSE 0 END) AS "數學",
SUM(CASE c.cname WHEN "英語" THEN sc.score ELSE 0 END) AS "英語",
SUM(CASE c.cname WHEN "物理" THEN sc.score ELSE 0 END) AS "物理",
IFNULL(AVG(sc.score),0) AS "平均分",
IFNULL(SUM(sc.score),0) AS "總分"
FROM student s
LEFT OUTER JOIN sc sc ON s.sid=sc.sid
LEFT OUTER JOIN course c ON sc.cid=c.cid
GROUP BY s.sid, s.sname
ORDER BY IFNULL(SUM(sc.score),0) DESC
LIMIT 0,4
(17)查詢總分排名在前二名到四名的學生所有成績單資訊(limit 1,3表示從第二條資料開始,連續三條資料)
SELECT s.sid AS "學號", s.sname AS "姓名",
SUM(CASE c.cname WHEN "語文" THEN sc.score ELSE 0 END) AS "語文",
SUM(CASE c.cname WHEN "數學" THEN sc.score ELSE 0 END) AS "數學",
SUM(CASE c.cname WHEN "英語" THEN sc.score ELSE 0 END) AS "英語",
SUM(CASE c.cname WHEN "物理" THEN sc.score ELSE 0 END) AS "物理",
IFNULL(AVG(sc.score),0) AS "平均分",
IFNULL(SUM(sc.score),0) AS "總分"
FROM student s
LEFT OUTER JOIN sc sc ON s.sid=sc.sid
LEFT OUTER JOIN course c ON sc.cid=c.cid
GROUP BY s.sid, s.sname
ORDER BY IFNULL(SUM(sc.score),0) DESC
LIMIT 1,3
(18)查詢學生平均成績及其名次
(非本人)
SELECT 1+(SELECT COUNT( distinct 平均成績)
FROM (
SELECT sid,AVG(score) AS 平均成績
FROM sc
GROUP BY sid ) AS T1
WHERE 平均成績 > T2.平均成績) as 名次, S# as 學生學號,平均成績
FROM (SELECT sid,AVG(score) 平均成績
FROM sc GROUP BY sid ) AS T2
ORDER BY 平均成績 desc