mysql sampdb數據庫存儲過程操作記錄
阿新 • • 發佈:2017-09-12
sampdb mysql 存儲過程
1.sampdb所用到的表格 sampdb關於學生考試的表格總共有四張,如下 學生信息表 MariaDB [sampdb]> select * from student; +-----------+-----+------------+ | name | sex | student_id | +-----------+-----+------------+ | Megan | F | 1 | | Joseph | M | 2 | | Kyle | M | 3 | 學生成績表 MariaDB [sampdb]> select * from score; +------------+----------+-------+ | student_id | event_id | score | +------------+----------+-------+ | 1 | 1 | 20 | | 3 | 1 | 20 | | 4 | 1 | 18 | 測試考試統計表 MariaDB [sampdb]> select * from grade_event; +------------+----------+----------+ | date | category | event_id | +------------+----------+----------+ | 2008-09-03 | Q | 1 | | 2008-09-06 | Q | 2 | | 2008-09-09 | T | 3 | | 2008-09-16 | Q | 4 | | 2008-09-23 | Q | 5 | | 2008-10-01 | T | 6 | +------------+----------+----------+ 學生缺勤表 MariaDB [sampdb]> select * from absence; +------------+------------+ | student_id | date | +------------+------------+ | 3 | 2008-09-03 | | 5 | 2008-09-03 | | 10 | 2008-09-06 | | 10 | 2008-09-09 | | 17 | 2008-09-07 | | 20 | 2008-09-07 | 2.表格結構索引 student | student | CREATE TABLE `student` ( `name` varchar(20) NOT NULL, `sex` enum(‘F‘,‘M‘) NOT NULL, `student_id` int(10) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`student_id`) ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=latin1 | score | score | CREATE TABLE `score` ( `student_id` int(10) unsigned NOT NULL, `event_id` int(10) unsigned NOT NULL, `score` int(11) NOT NULL, PRIMARY KEY (`event_id`,`student_id`), KEY `student_id` (`student_id`), CONSTRAINT `score_ibfk_1` FOREIGN KEY (`event_id`) REFERENCES `grade_event` (`event_id`), CONSTRAINT `score_ibfk_2` FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | grade_event | grade_event | CREATE TABLE `grade_event` ( `date` date NOT NULL, `category` enum(‘T‘,‘Q‘) NOT NULL, `event_id` int(10) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`event_id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 | absence | absence | CREATE TABLE `absence` ( `student_id` int(10) unsigned NOT NULL, `date` date NOT NULL, PRIMARY KEY (`student_id`,`date`), CONSTRAINT `absence_ibfk_1` FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | 3.檢索數據 檢索統計參加event_id為1考試的學生人數 MariaDB [sampdb]> select count(student_id) from score where event_id=1; +-------------------+ | count(student_id) | +-------------------+ | 29 | +-------------------+ 1 row in set (0.00 sec) 列出參加event_id為1考試的學生名單 MariaDB [sampdb]> select group_concat(student.name) from student left join score on student.student_id=score.student_id where score.event_id=1; +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | group_concat(student.name) | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Megan,Kyle,Katie,Abby,Nathan,Liesl,Ian,Colin,Peter,Michael,Thomas,Ben,Aubrey,Rebecca,Will,Max,Rianne,Avery,Lauren, Becca,Gregory,Sarah,Robbie,Keaton,Carter,Teddy,Gabrielle,Grace,Emily | +--------- 檢索統計未參加event_id為1考試的學生人數 MariaDB [sampdb]> select count(student.student_id) from student left join (select * from score where event_id=1) as score1 on student.student_id=score1.student_id where score1.score is null; +---------------------------+ | count(student.student_id) | +---------------------------+ | 2 | +---------------------------+ 1 row in set (0.00 sec) 列出未參加event_id為1考試的學生名單 MariaDB [sampdb]> select group_concat(student.name) from student left join (select * from score where event_id=1) as score1 on student.student_id=score1.student_id where score1.score is null; +----------------------------+ | group_concat(student.name) | +----------------------------+ | Joseph,Devri | +----------------------------+ 檢索參加event_id為1考試的學生最高成績,平均成績,最低成績 MariaDB [sampdb]> select max(score1.score),avg(score1.score),min(score1.score) from student left join (select * from score where event_id=1) as score1 on student.student_id=score1.student_id; +-------------------+-------------------+-------------------+ | max(score1.score) | avg(score1.score) | min(score1.score) | +-------------------+-------------------+-------------------+ | 20 | 15.1379 | 9 | +-------------------+-------------------+-------------------+ 檢索參加event_id為1考試的最高成績的學生名字 select student.name from student left join score on student.student_id=score.student_id where score.event_id=1 and score=(select max(score) from score where event_id=1); +--------+ | name | +--------+ | Megan | | Kyle | | Aubrey | | Max | +--------+ 減速參加event_id為1考試的最低成績的學生名字 MariaDB [sampdb]> select student.name,score from student left join score on student.student_id=score.student_id where score.event_id=1 and score=(select min(score) from score where event_id=1); +--------+-------+ | name | score | +--------+-------+ | Will | 9 | | Rianne | 9 | | Avery | 9 | +--------+-------+ 3 rows in set (0.00 sec) 4.寫一個存儲過程,根據考試ID,列出這次考試多少人參加,列出參加學生的名字,多少人缺席,列出缺席的學生名字, 列出考試的最高成績的獲得者的名字和最高成績,最低成績的獲得者名字和最低成績,平均成績。 create procedure id_in(num int) begin select concat(‘考試人數‘),count(student_id) from score where event_id=num; select concat(‘參加考試名單‘),group_concat(name) from student left join score on student.student_id=score.student_id where event_id=num; select concat(‘缺席考試人數‘),count(name) from student left join (select * from score where event_id=num) as score1 on student.student_id=score1.student_id where score1.score is null; select concat(‘缺席考試名單‘),group_concat(name) from student left join (select * from score where event_id=num) as score1 on student.student_id=score1.student_id where score1.score is null; select concat(‘最高成績學生名單‘),group_concat(student.name) from student left join score on student.student_id=score.student_id where score.event_id=num and score=(select max(score) from score where score.event_id=num); select concat(‘最 低成績學生名單‘),group_concat(student.name) from student left join score on student.student_id=score.student_id where score.event_id=num and score=(select min(score) from score where score.event_id=num); select concat(‘最高成績‘),max(score1.score),concat(‘平均成績‘),avg(score1.score),concat(‘最低成績‘),min(score1.score) from student left join (select * from score where score.event_id=num) as score1 on student.student_id=score1.student_id; end$ 結果: MariaDB [sampdb]> call id_in(1); +------------------------+-------------------+ | concat(‘考試人數‘) | count(student_id) | +------------------------+-------------------+ | 考試人數 | 29 | +------------------------+-------------------+ 1 row in set (0.01 sec) +------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | concat(‘參加考試名單‘) | group_concat(name) | +------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 參加考試名單 | Megan,Kyle,Katie,Abby,Nathan,Liesl,Ian,Colin,Peter,Michael,Thomas,Ben,Aubrey,Rebecca,Will,Max,Rianne,Avery,Lauren,Becca,Gregory,Sarah,Robbie,Keaton,Carter,Teddy,Gabrielle,Grace,Emily | +------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) +------------------------------+-------------+ | concat(‘缺席考試人數‘) | count(name) | +------------------------------+-------------+ | 缺席考試人數 | 2 | +------------------------------+-------------+ 1 row in set (0.01 sec) +------------------------------+--------------------+ | concat(‘缺席考試名單‘) | group_concat(name) | +------------------------------+--------------------+ | 缺席考試名單 | Joseph,Devri | +------------------------------+--------------------+ 1 row in set (0.01 sec) +------------------------------------+----------------------------+ | concat(‘最高成績學生名單‘) | group_concat(student.name) | +------------------------------------+----------------------------+ | 最高成績學生名單 | Megan,Kyle,Aubrey,Max | +------------------------------------+----------------------------+ 1 row in set (0.01 sec) +-------------------------------------+----------------------------+ | concat(‘最 低成績學生名單‘) | group_concat(student.name) | +-------------------------------------+----------------------------+ | 最 低成績學生名單 | Will,Rianne,Avery | +-------------------------------------+----------------------------+ 1 row in set (0.01 sec) +------------------------+-------------------+------------------------+-------------------+------------------------+-------------------+ | concat(‘最高成績‘) | max(score1.score) | concat(‘平均成績‘) | avg(score1.score) | concat(‘最低成績‘) | min(score1.score) | +------------------------+-------------------+------------------------+-------------------+------------------------+-------------------+ | 最高成績 | 20 | 平均成績 | 15.1379 | 最低成績 | 9 | +------------------------+-------------------+------------------------+-------------------+------------------------+-------------------+ 1 row in set (0.01 sec) Query OK, 0 rows affected (0.01 sec)
本文出自 “gome學習” 博客,請務必保留此出處http://goome.blog.51cto.com/4045241/1964559
mysql sampdb數據庫存儲過程操作記錄