mysql sampdb資料庫預存程序操作記錄

來源:互聯網
上載者:User

標籤: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)beginselect 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資料庫預存程序操作記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.