1. 程式人生 > >mysql的案例和題目

mysql的案例和題目

mysql的案例和題目

用到如下幾張表

直接po程式碼

#mysql案例和題目

#一、查詢每個專業的學生人數
#
SELECT stu.`majorid`, COUNT(*) AS "人數" FROM 
student stu GROUP BY stu.`majorid`;

#最終效果
SELECT m.*, 
(
SELECT COUNT(*) FROM student stu 
WHERE m.`majorid` = stu.majorid
GROUP BY stu.`majorid`
) AS "人數"
FROM major m;

#
DESC student;
#二、查詢參加考試的學生中,每個學生的平均分、最高分
SELECT * FROM result;
#
SELECT studentno, AVG(score), MAX(score) FROM 
result GROUP BY `studentno`;

#三、查詢姓張的每個學生的最低分大於60的學號、姓名
SELECT studentno FROM student WHERE studentname LIKE '張%';
#
SELECT * FROM result WHERE studentno IN (
SELECT studentno FROM student WHERE studentname LIKE '張%');
#
SELECT MIN(score) AS min_sco, studentno FROM result WHERE studentno IN (
SELECT studentno FROM student WHERE studentname LIKE '張%')
GROUP BY studentno
HAVING min_sco > 60;
#最終效果
SELECT studentno, studentname FROM student WHERE studentno IN (
SELECT studentno FROM result WHERE studentno IN (
SELECT studentno FROM student WHERE studentname LIKE '張%')
GROUP BY studentno
HAVING MIN(score) > 60);
#

#四、查詢生日在“1988-1-1”後的學生姓名、專業名稱
SELECT * FROM student WHERE borndate > '1988-1-1';
#
SELECT studentname, majorname FROM student stu
INNER JOIN major m
ON stu.`majorid` = m.`majorid`
WHERE stu.`borndate` > '1988-1-1';
#或者用DATEDIFF()函式
SELECT studentname, majorname FROM student stu
INNER JOIN major m
ON stu.`majorid` = m.`majorid`
WHERE DATEDIFF(stu.`borndate`, '1988-1-1') > 0;


#五、查詢每個專業的男生人數和女生人數分別是多少
SELECT majorid, sex, COUNT(*) AS "人數" FROM 
student GROUP BY majorid, sex;
#或者
SELECT stu.majorid,
(SELECT COUNT(*) FROM student WHERE sex = '男' AND student.`majorid` = stu.`majorid`) AS boy,
(SELECT COUNT(*) FROM student WHERE sex = '女' AND student.`majorid` = stu.`majorid`) AS girl
FROM student stu GROUP BY stu.majorid, boy, girl; 


#六、查詢專業和張翠山一樣的學生的最低分
SELECT MIN(score) FROM result WHERE studentno IN (
SELECT studentno FROM student WHERE majorid IN (
SELECT DISTINCT majorid FROM student WHERE studentname = '張翠山'));

#七、查詢大於60分的學生的姓名、密碼、專業名
#三表連線
SELECT stu.`studentname`, stu.`loginpwd`, m.`majorname` FROM student AS stu
INNER JOIN major AS m
ON stu.`majorid` = m.`majorid`
INNER JOIN result AS r
ON stu.`studentno` = r.`studentno`
WHERE r.`score` > 60;

#八、按郵箱位數分組,查詢每組的學生個數
SELECT LENGTH(email) AS len_ema, email FROM student;
#
SELECT LENGTH(email) AS len_ema, COUNT(*) FROM student GROUP BY len_ema;

#九、查詢學生名、專業名、分數
#三表連線
SELECT stu.`studentname`, r.`score`, m.`majorname` FROM student AS stu
INNER JOIN major AS m
ON stu.`majorid` = m.`majorid`
INNER JOIN result AS r
ON stu.`studentno` = r.`studentno`;
#有的人沒有分數,加外連線就可以了
SELECT stu.`studentname`, r.`score`, m.`majorname` FROM student AS stu
INNER JOIN major AS m
ON stu.`majorid` = m.`majorid`
LEFT OUTER JOIN result AS r
ON stu.`studentno` = r.`studentno`;

#十、查詢哪個專業沒有學生,分別用左連線和右連線實現
#左外聯接
SELECT * FROM major m LEFT OUTER JOIN student stu
ON m.`majorid` = stu.`majorid`
WHERE stu.`studentno` IS NULL;
#右外聯接
SELECT * FROM student stu RIGHT OUTER JOIN major m
ON m.`majorid` = stu.`majorid`
WHERE stu.`studentno` IS NULL;

#十一、查詢沒有成績的學生人數
SELECT COUNT(*) FROM student AS s
LEFT OUTER JOIN result AS r
ON s.`studentno` = r.`studentno`
WHERE r.`id` IS NULL;
#