1. 程式人生 > 其它 >MySql(4)-select(2)

MySql(4)-select(2)

--==================分頁和排序================
--升序 ASC   降序 DESC       LIMIT 起始值,每頁的數量 
--查詢Java程式設計-1 課程成績排名前十的學生,並且分數大於八十(學號、姓名、課程名稱、分數)
SELECT s.studentno,studentname,subjectname, studentresult
FROM student AS s
JOIN result AS r
ON s.studentno = r.studentno
JOIN SUBJECT AS sub
ON r.subjectno = sub.subjectno
WHERE studentresult>80 AND subjectname='Java程式設計-1' ORDER BY studentresult ASC LIMIT 0,2 --LIMIT 0,2 就是從第一條資訊開始,頁面有兩條資訊 LIMIT 2,2 從第三條資訊開始,頁面有兩條資訊
--===============子查詢和巢狀查詢 (由裡及外)====================
--Java程式設計-1課程分數不小於80分的學生的學號和姓名 
SELECT studentno,studentname
FROM student
WHERE studentno IN (
    
SELECT studentno FROM result WHERE studentresult>=80 AND subjectno IN ( SELECT subjectno FROM `subject` WHERE subjectname = 'Java程式設計-1')) --查詢 Java程式設計-1 前五名同學的成績資訊(學號 姓名 分數) SELECT s.studentno,studentname,studentresult FROM student s JOIN result r ON s.studentno = r.studentno WHERE
subjectno IN ( SELECT subjectno FROM `subject` WHERE subjectname = 'Java程式設計-1') ORDER BY studentresult DESC LIMIT 0,5
--================聚合函式=========================
--COUNT 計數  
SELECT COUNT(sex) FROM student  --COUNT(欄位),會忽略所有的NULL值
SELECT COUNT(*) FROM student   --不會忽略NULL值
SELECT COUNT(1) FROM result   --不會忽略NULL值

SELECT SUM(studentresult) AS 總和 FROM result
SELECT AVG(studentresult) AS 平均分 FROM result 
SELECT MAX(studentresult) AS 最高分 FROM result
SELECT MIN(studentresult) AS 最低分 FROM result

--查詢不同課程的平均分、最高分、最低分   (分組)
SELECT subjectname,AVG(studentresult),MAX(studentresult),MIN(studentresult)
FROM result r
JOIN `subject` sub
ON r.subjectno=sub.subjectno
GROUP BY sub.subjectno      --通過什麼欄位來分組
HAVING AVG(studentresult)>60   --HAVING的條件必須寫在GROUP BY的下面
--===================MD5加密========================
UPDATE student SET loginpwd = MD5(loginpwd) WHERE studentno=1000