MySQL基础作业三

ops/2024/10/19 5:28:26/

查询
1.分别查询student表和score表的所有记录

mysql> select *from student;
+-----+--------+------+-------+------------+--------------+
| id  | name   | sex  | birth | department | address      |
+-----+--------+------+-------+------------+--------------+
| 901 | 张三丰 | 男   |  2002 | 计算机系   | 北京市海淀区 |
| 902 | 周全有 | 男   |  2000 | 中文系     | 北京市昌平区 |
| 903 | 张思维 | 女   |  2003 | 中文系     | 湖南省永州市 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 |
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市 |
| 906 | 王心凌 | 女   |  1998 | 计算机系   | 湖南省衡阳市 |
+-----+--------+------+-------+------------+--------------+
mysql> select *from score;
+-----+--------+--------+-------+
| id  | stu_id | c_name | grade |
+-----+--------+--------+-------+
| 106 |    901 | 计算机 |    98 |
| 107 |    901 | 英语   |    80 |
| 108 |    902 | 计算机 |    65 |
| 109 |    902 | 中文   |    88 |
| 110 |    903 | 中文   |    95 |
| 111 |    904 | 计算机 |    70 |
| 112 |    904 | 英语   |    92 |
| 113 |    905 | 英语   |    94 |
| 114 |    906 | 计算机 |    49 |
| 115 |    906 | 英语   |    83 |
+-----+--------+--------+-------+

2.查询student表的第2条到5条记录

limit  初始位置 , 记录数  #从第几条开始显示,显示几条  注意:第一条记录的位置是0

limit  记录数;  #从第一条记录开始显示几条记录 

mysql> select *from student limit 1,4;
+-----+--------+------+-------+------------+--------------+
| id  | name   | sex  | birth | department | address      |
+-----+--------+------+-------+------------+--------------+
| 902 | 周全有 | 男   |  2000 | 中文系     | 北京市昌平区 |
| 903 | 张思维 | 女   |  2003 | 中文系     | 湖南省永州市 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 |
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市 |
+-----+--------+------+-------+------------+--------------+

3.从student表中查询计算机系和英语系的学生的信息

mysql> select *from student where  department='计算机系' or  depart
ment ='英语系';

4.从student表中查询年龄小于22岁的学生信息

mysql> select *from student where  (year(now())-birth) <22;
+-----+--------+------+-------+------------+--------------+
| id  | name   | sex  | birth | department | address      |
+-----+--------+------+-------+------------+--------------+
| 903 | 张思维 | 女   |  2003 | 中文系     | 湖南省永州市 |
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市 |
+-----+--------+------+-------+------------+--------------+

5.从student表中查询每个院系有多少人

mysql> select department,count(1)'总人数' from student group by dep
artment;
+------------+--------+
| department | 总人数 |
+------------+--------+
| 计算机系   |      2 |
| 中文系     |      2 |
| 英语系     |      2 |
+------------+--------+

6.从score表中查询每个科目的最高分

mysql> select c_name 科目,max(grade)'最高分' from score  group by c
_name;
+--------+--------+
| 科目   | 最高分 |
+--------+--------+
| 计算机 |     98 |
| 英语   |     94 |
| 中文   |     95 |
+--------+--------+

7.查询李广昌的考试科目(cname)和考试成绩(grade)

mysql> select c_name,grade from student a join score b on a.id=b.stu_id where name = '李广昌';
+--------+-------+
| c_name | grade |
+--------+-------+
| 计算机 |    70 |
| 英语   |    92 |
+--------+-------+

8.用连接的方式查询所有学生的信息和考试信息

mysql> select *from student a join score b on a.id=b.stu_id ;

9.计算每个学生的总成绩

mysql> select name , sum(grade) '总成绩' from student a join score b on a.i
d=b.stu_id group by stu_id;
+--------+--------+
| name   | 总成绩 |
+--------+--------+
| 张三丰 |    178 |
| 周全有 |    153 |
| 张思维 |     95 |
| 李广昌 |    162 |
| 王翰   |     94 |
| 王心凌 |    132 |
+--------+--------+

10.计算每个考试科目的平均成绩

mysql> select c_name,round(avg(grade),1)'平均成绩' from score group by c_na
me;
+--------+----------+
| c_name | 平均成绩 |
+--------+----------+
| 计算机 |     70.5 |
| 英语   |     87.3 |
| 中文   |     91.5 |
+--------+----------+

11.查询计算机成绩低于95的学生信息

mysql> select *from student a ,(select stu_id,grade from score where c_name
='计算机' and grade <95) stu where a.id = stu.stu_id;
+-----+--------+------+-------+------------+--------------+--------+-------+
| id  | name   | sex  | birth | department | address      | stu_id | grade |
+-----+--------+------+-------+------------+--------------+--------+-------+
| 902 | 周全有 | 男   |  2000 | 中文系     | 北京市昌平区 |    902 |    65 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 |    904 |    70 |
| 906 | 王心凌 | 女   |  1998 | 计算机系   | 湖南省衡阳市 |    906 |    49 |
+-----+--------+------+-------+------------+--------------+--------+-------+
mysql> select a.*,c_name,grade from student a join score b on a.id=b.stu_id where c_name='计算机' and grade <95;
+-----+--------+------+-------+------------+--------------+--------+-------+
| id  | name   | sex  | birth | department | address      | c_name | grade |
+-----+--------+------+-------+------------+--------------+--------+-------+
| 902 | 周全有 | 男   |  2000 | 中文系     | 北京市昌平区 | 计算机 |    65 |
| 904 | 李广昌 | 男   |  1999 | 英语系     | 辽宁省皋新市 | 计算机 |    70 |
| 906 | 王心凌 | 女   |  1998 | 计算机系   | 湖南省衡阳市 | 计算机 |    49 |
+-----+--------+------+-------+------------+--------------+--------+-------+

12.将计算机考试成绩按从高到低进行排序

mysql> select grade'计算机成绩' from score where c_name='计算机' order by grade desc;
+------------+
| 计算机成绩 |
+------------+
|         98 |
|         70 |
|         65 |
|         49 |

13.从student表和score表中查询出学生的学号,然后合并查询结果 

mysql> select distinct a.id from  student a join score b on a.id=b.stu_id;
+-----+
| id  |
+-----+
| 901 |
| 902 |
| 903 |
| 904 |
| 905 |
| 906 |
+-----+

14.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

mysql> select name,department,c_name ,grade from  student a join score b on a.id=b.stu_id wher
e name like '张%' or  name like '王%';
+--------+------------+--------+-------+
| name   | department | c_name | grade |
+--------+------------+--------+-------+
| 张三丰 | 计算机系   | 计算机 |    98 |
| 张三丰 | 计算机系   | 英语   |    80 |
| 张思维 | 中文系     | 中文   |    95 |
| 王翰   | 英语系     | 英语   |    94 |
| 王心凌 | 计算机系   | 计算机 |    49 |
| 王心凌 | 计算机系   | 英语   |    83 |
+--------+------------+--------+-------+

15.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

mysql> select name ,year(now())-birth'age', address,department,c_name,grade  from  student a j
oin score b on a.id=b.stu_id where address like '湖南%';
+--------+------+--------------+------------+--------+-------+
| name   | age  | address      | department | c_name | grade |
+--------+------+--------------+------------+--------+-------+
| 张思维 |   21 | 湖南省永州市 | 中文系     | 中文   |    95 |
| 王心凌 |   26 | 湖南省衡阳市 | 计算机系   | 计算机 |    49 |
| 王心凌 |   26 | 湖南省衡阳市 | 计算机系   | 英语   |    83 |
+--------+------+--------------+------------+--------+-------+


http://www.ppmy.cn/ops/110211.html

相关文章

2.3.2 协程调度器实现与性能测试

LINUX 精通 8 day24 20240909 晚19&#xff1a;35 - 20: 47 课程链接地址 老师画图用的是excalidraw 可以在线 本地&#xff01; Excalidraw&#xff1a;开源实用的白板画图工具&#xff08;在线/本地安装&#xff09;-CSDN博客 2.3.2 协程调度器实现与性能测试 复习了上…

等保测评中的访问控制策略:企业优化指南

在信息安全等级保护&#xff08;等保&#xff09;测评中&#xff0c;访问控制作等保测评中的访问控制策略&#xff1a;企业优化指南为保障信息系统安全的关键环节&#xff0c;其策略的合理性和有效性直接影响到测评结果。企业如何优化访问控制策略&#xff0c;以满足等保测评的…

苍穹外卖——day1

前后端联调 前端先传给nginx再传给后端服务器 反向代理 我们可以看到前端和后端的访问地址不一样&#xff0c;那么怎么实现进行访问的呢&#xff1f; 我们是通过一种反向代理技术来实现的 利用nginx反向代理来实现前后端联调 下面是如何在我们的idea项目中使用nginx反向代理…

android studio 模拟器 loadlibrary failed with 126:找不到指定的模块

loadlibrary failed with 126:找不到指定的模块 解决方法 解决方法&#xff1a;设备管理器-> 显示适配器-> 禁用 AMD Redeon 重启AndroidStudio

小杨的H字矩阵小杨的日字矩阵 c++

小杨的H字矩阵 题目描述 小杨想要构造一个NxN的H字矩阵(N为奇数)&#xff0c;具体来说&#xff0c;这个矩阵共有N行&#xff0c;每行N个字符&#xff0c;其中最左列、最右列都是 | &#xff08;键盘右侧删除键下回车键上&#xff0c;shift\&#xff09;&#xff0c;而中间一行…

STM32单片机 定时器TIM输出比较 PWM波形

一. OC&#xff08;Output Compare&#xff09;输出比较 了解&#xff1a;IC&#xff08;Input Capture&#xff09;输入捕获、CC&#xff08;Capture/Compare&#xff09;输入捕获和输出比较单元OC功能&#xff1a;用来输出PWM波形&#xff0c;PWM波形又是用来驱动电机的必要…

uniapp 各个端接入腾讯滑动行为验证码示例

验证调起页面&#xff1a; <template><view class"app"><text>{{ obj.ret }}</text><button click"varify">验证</button></view> </template><script>export default{data(){return{obj: {}}},on…

项目总结 普通模式 相框模式

目录 一、相框选择对话框 1、数据结构定义 1)、模式枚举 2)、相框模式下,不显示连麦者时,确定具体显示模式 3)、初始化布局 4)、转换布局 5)、手动切换布局 6)、切换时机 6.1)、右键菜单 6.2)、相框模式选择 2、跳出相框模式对话框时机 0)、初始化 1)、…