1. 程式人生 > >Mysql進行復雜查詢

Mysql進行復雜查詢

1.查詢“生物”課程比“物理”課程成績高的所有學生的學號;

 思路: (1)獲取所有選了 生物 課程的學生的成績(學號,成績) --臨時表

      (2)獲取所有選了 物理 課程的學生的成績(學號,成績) --臨時表

   (3)根據學號連線兩張臨時表(學號,生物成績,物理成績),加條件進行查詢

複製程式碼
SELECT
    A.student_id AS 學號,
    sw AS 生物,
    wl AS 物理
FROM
    (
        SELECT
            student_id,
            num AS sw
        FROM
            score
        
LEFT JOIN course ON score.course_id = course.cid WHERE course.cname = '生物' ) AS A LEFT JOIN ( SELECT student_id, num AS wl FROM score LEFT JOIN course ON score.course_id = course.cid WHERE course.cname = '物理' ) AS B ON A.student_id =
B.student_id WHERE sw > IF (isnull(wl), 0, wl);
複製程式碼

2.查詢平均成績大於60分的同學的學號和平均成績;

思路:(1)根據學號分組

    (2)使用avg()聚合函式計算平均成績

    (3)通過having對平均成績進行篩選

SELECT student_id,avg(num) FROM score
LEFT JOIN course
ON score.student_id=course.cid
GROUP BY student_id
HAVING avg(num)>60

3.查詢所有同學的學號、姓名、選課數、總成績;

思路:根據學號分組,使用count()對選課數計數,sum()計算總成績

複製程式碼
SELECT
    student_id,
    sname,
    count(student_id),
    sum(num)
FROM
  score LEFT JOIN student ON score.student_id = student.sid
GROUP BY student_id
複製程式碼

4.查詢姓“李”的老師的個數;

思路:使用like及萬用字元匹配,count()進行計數

SELECT count(tid) FROM teacher
WHERE tname LIKE '李%'

5.查詢沒學過“李平”老師課的同學的學號、姓名;

思路:(1)連線成績表 課程表 教師表得到選了李平老師課程的學生

         (2)再通過學生表篩選結果

複製程式碼
SELECT sid,sname FROM student 
WHERE sid not IN
 (SELECT student_id FROM 
    (SELECT cid,teacher_id,student_id,course_id
         FROM score 
        LEFT JOIN course ON score.course_id=course.cid 
    ) AS A 
    LEFT JOIN teacher 
    ON A.teacher_id=teacher.tid where teacher.tname='李平老師'  GROUP BY student_id
)
複製程式碼

6.查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名;

思路:(1)篩選出學過001課程的學生或學過002課程的學生

    (2)根據學生分組,如果學生數量等於2,則該學生選擇了以上兩門課程

SELECT student.sid,student.sname FROM 
(SELECT student_id,count(student_id) FROM score LEFT JOIN course ON score.course_id=course.cid  WHERE course.cid='001' or course.cid='002' GROUP BY student_id HAVING count(student_id)>1 ) AS A 
LEFT JOIN student ON A.student_id=student.sid;

7.查詢學過“李平”老師所教的所有課的同學的學號、姓名;

思路:(1)查詢李平老師所教的課程

         (2)在成績表中篩選出學生選擇的課程 in 李平老師的課程

複製程式碼
SELECT student_id,sname FROM 
(SELECT student_id FROM score
WHERE course_id IN
(SELECT cid FROM teacher LEFT JOIN course ON teacher.tid=course.teacher_id WHERE tname='李平老師')
GROUP BY student_id
) AS B
LEFT JOIN student
ON B.student_id=student.sid
複製程式碼

8.查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名;

思路:(1)分別獲取選擇了 課程001和002的學生和成績;

   (2)連線兩張表,篩選出001的成績大於002成績的學生

複製程式碼
SELECT student_id,sname FROM
(SELECT A.student_id FROM
(SELECT student_id,num FROM score WHERE course_id=001) AS A
LEFT JOIN
(SELECT student_id,num FROM score WHERE course_id=002) AS B
ON A.student_id=B.student_id
WHERE A.num>B.num) AS C
LEFT JOIN student
ON C.student_id=student.sid
複製程式碼

9.查詢有課程成績小於60分的同學的學號、姓名;

思路:(1)篩選出成績小於60的學生,並通過學生分組   --臨時表

    (2)在學生表中篩選出 in 臨時表中的學生

SELECT sid,sname FROM student WHERE sid IN
(SELECT student_id FROM score WHERE num<60 GROUP BY student_id)

10.查詢沒有學全所有課的同學的學號、姓名;

思路:(1)統計出總課程數

         (2)成績表中,通過學生分組,統計出每個學生的課程數,如果課程數等於總課程數,則表示選擇了所有課程

SELECT sid,sname FROM student WHERE sid not IN
(SELECT student_id
FROM score
GROUP BY student_id HAVING count(course_id) = (SELECT count(cid) FROM course))

11.查詢至少有一門課與學號為“001”的同學所學課程相同的同學的學號和姓名;

思路:(1)查詢學號001同學所學的所有課程  --臨時表

    (2)其他學生所學的課程如果在臨時表中,則符合條件

SELECT student_id,sname FROM student LEFT JOIN score ON score.student_id=student.sid
WHERE course_id in (SELECT course_id FROM score WHERE student_id=001) AND student_id != 001
GROUP BY student_id

12.查詢至少學過學號為“001”同學所選課程中任意一門課的其他同學學號和姓名;

13.查詢和“002”號的同學學習的課程完全相同的其他同學學號和姓名;

14.刪除學習“葉平”老師課的成績表記錄;

DELETE FROM score WHERE course_id IN
(SELECT cid FROM course LEFT JOIN teacher ON course.teacher_id=teacher.tid WHERE tname='葉平老師')

15.向成績表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“002”課程的同學學號;②插入“002”號課程的平均成績;

16.按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;

複製程式碼
select sc.student_id,
        (select num from score left join course on score.course_id = course.cid where course.cname = "生物" and score.student_id=sc.student_id) as sy,
        (select num from score left join course on score.course_id = course.cid where course.cname = "物理" and score.student_id=sc.student_id) as wl,
        (select num from score left join course on score.course_id = course.cid where course.cname = "體育" and score.student_id=sc.student_id) as ty,
        count(sc.course_id),
        avg(sc.num)
    from score as sc
    group by student_id desc
複製程式碼

17.查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;

SELECT course_id,max(num) as 最高分,min(num) as 最低分
FROM score
GROUP BY course_id

18.按各科平均成績從低到高和及格率的百分數從高到低順序;

思路:三元運算(三目運算),case .. when .. then .. else .. end

SELECT course_id,avg(num) AS 平均分,sum(CASE WHEN score.num>60 THEN 1 ELSE 0 END)/count(1)*100 AS 及格率 FROM score GROUP BY course_id
ORDER BY 平均分 ASC,及格率 DESC 

19.課程平均分從高到低顯示(顯示任課老師);

SELECT course_id,avg(num),teacher.tname FROM score LEFT JOIN course ON score.course_id=course.cid LEFT JOIN teacher ON course.teacher_id=teacher.tid
GROUP BY course_id
ORDER BY avg(num) DESC

20.查詢各科成績前三名的記錄:(不考慮成績並列情況) ;

21.查詢每門課程被選修的學生數;

SELECT course_id,count(student_id) FROM score
GROUP BY course_id

22.查詢出只選修了一門課程的全部學生的學號和姓名;

SELECT student_id,student.sname FROM score LEFT JOIN student ON score.student_id=student.sid
GROUP BY student_id
HAVING count(student_id)=1

23.查詢男生、女生的人數;

SELECT 
  (SELECT count(1) FROM student WHERE gender='') AS 男,
  (SELECT count(1) FROM student WHERE gender='') As

24.查詢姓“張”的學生名單;

SELECT * FROM student WHERE student.sname LIKE '張%'

25.查詢同名同姓學生名單,並統計同名人數;

SELECT sname,count(sname) FROM student GROUP BY sname HAVING count(sname)>1

26.查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;

SELECT course_id,avg(num) FROM score GROUP BY course_id ORDER BY course_id ASC,course_id DESC

27.查詢平均成績大於85的所有學生的學號、姓名和平均成績;

SELECT student_id,sname,avg(num) FROM score LEFT JOIN student ON score.student_id=student.sid
GROUP BY student_id HAVING avg(num)>85

28.查詢課程名稱為“數學”,且分數低於60的學生姓名和分數;

SELECT sname,num FROM score LEFT JOIN course ON score.course_id=course.cid LEFT JOIN student ON score.student_id=student.sid  WHERE cname='數學' AND num>60

29.查詢課程編號為003且課程成績在80分以上的學生的學號和姓名;

SELECT student_id,sname FROM score LEFT JOIN student ON score.student_id=student.sid WHERE course_id=003 AND num>80

30.求選了課程的學生人數

select count(distinct student_id) from score

31.查詢選修“楊豔”老師所授課程的學生中,成績最高的學生姓名及其成績;

思路:根據學生排序,成績按從大到小排序,limit取最高的成績

複製程式碼
SELECT sname,max(num) FROM score LEFT JOIN course ON score.course_id=course.cid LEFT JOIN teacher ON course.teacher_id=teacher.tid
LEFT JOIN student ON score.student_id=student.sid
WHERE tname='張磊老師'
GROUP BY student_id
ORDER BY max(num) DESC
LIMIT 1
複製程式碼

32.查詢各個課程及相應的選修人數;

SELECT cid,cname,count(student_id) FROM score LEFT JOIN course ON score.course_id=course.cid
GROUP BY course_id

33.查詢不同課程但成績相同的學生的學號、課程號、學生成績;

select DISTINCT s1.course_id,s2.course_id,s1.num,s2.num from
   score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id;

34.查詢每門課程成績最好的前兩名;

複製程式碼
SELECT * FROM score LEFT JOIN
(SELECT course_id,
 (SELECT num FROM score WHERE score.course_id=A.course_id ORDER BY num DESC LIMIT 0,1) AS '第一名',
    (SELECT num FROM score WHERE score.course_id=A.course_id ORDER BY num DESC LIMIT 1,1) AS '第二名'
FROM score AS A
GROUP BY course_id) AS B
ON score.course_id=B.course_id
複製程式碼

35.檢索至少選修兩門課程的學生學號;

思路:根據學號分組,統計

SELECT student_id,count(course_id) FROM score GROUP BY student_id HAVING count(course_id)>=2

36.查詢全部學生都選修的課程的課程號和課程名;

思路:從學生表中統計出學生總數,在成績表中根據課程分組,如果選擇沒門課程的人數等於學生總數,則符合

SELECT course_id,cname FROM score LEFT JOIN course ON score.course_id=course.cid GROUP BY course_id HAVING count(student_id)=
(SELECT count(sid) FROM student)

37.查詢沒學過“葉平”老師講授的任一門課程的學生姓名;

SELECT sname FROM score LEFT JOIN student ON score.student_id=student.sid WHERE course_id NOT IN
(SELECT cid FROM teacher LEFT JOIN course ON course.teacher_id=teacher.tid WHERE tname='葉平老師')

38.查詢兩門以上不及格課程的同學的學號及其平均成績;

思路:通過學生分組,篩選出不及格課程數

SELECT student_id,avg(num) FROM score WHERE num<60 GROUP BY student_id HAVING count(1)>2

39.檢索“004”課程分數小於60,按分數降序排列的同學學號;

SELECT student_id FROM score WHERE course_id=004 AND num<60 ORDER BY num DESC

40.刪除“002”同學的“001”課程的成績;

DELETE FROM score WHERE student_id=002 AND course_id=001