当前位置: 首页 > news >正文

MYSQL数据库--基本练习

(1)分别查询student表和score表得所有记录

mysql> select * from score;
+----+--------+-----------+-------+
| id | stu_id | c_name    | grade |
+----+--------+-----------+-------+
|  1 |    901 | 计算机    |    98 |
|  2 |    901 | 英语      |    80 |
|  3 |    902 | 计算机    |    65 |
|  4 |    902 | 中文      |    88 |
|  5 |    903 | 中文      |    95 |
|  6 |    904 | 计算机    |    70 |
|  7 |    904 | 英语      |    92 |
|  8 |    905 | 英语      |    94 |
|  9 |    906 | 计算机    |    49 |
| 10 |    906 | 英语      |    83 |
+----+--------+-----------+-------+
10 rows in set (0.07 sec)mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张三丰    | 男   |  2002 | 计算机系     | 北京市海淀区       |
| 902 | 周全有    | 男   |  2000 | 中文系       | 北京市昌平区       |
| 903 | 张思维    | 女   |  2003 | 中文系       | 湖南省永州市       |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省皋新市       |
| 905 | 王翰      | 男   |  2004 | 英语系       | 福建省厦门市       |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.05 sec)

(2)查询student表得第二条到第五条记录

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

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

mysql> select * from student where department='计算机系' or department='英语系';
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张三丰    | 男   |  2002 | 计算机系     | 北京市海淀区       |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省皋新市       |
| 905 | 王翰      | 男   |  2004 | 英语系       | 福建省厦门市       |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.00 sec)

(4)从student表中查询年龄小于22岁的学生信息   两个方法

mysql> select * from student where 2025-birth < 22;
+-----+--------+------+-------+------------+--------------------+
| id  | name   | sex  | birth | department | address            |
+-----+--------+------+-------+------------+--------------------+
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市       |
+-----+--------+------+-------+------------+--------------------+
1 row in set (0.00 sec)mysql> select *, year(now())-birth age from student where year(now())-birth < 22;
+-----+--------+------+-------+------------+--------------------+------+
| id  | name   | sex  | birth | department | address            | age  |
+-----+--------+------+-------+------------+--------------------+------+
| 905 | 王翰   | 男   |  2004 | 英语系     | 福建省厦门市       |   21 |
+-----+--------+------+-------+------------+--------------------+------+
1 row in set (0.00 sec)

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

mysql> select department,count(*) '人数' from student group by department;
+--------------+--------+
| department   | 人数   |
+--------------+--------+
| 计算机系     |      2 |
| 中文系       |      2 |
| 英语系       |      2 |
+--------------+--------+
3 rows in set (0.07 sec)

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

mysql> select c_name,max(grade) '最高分' from score group by c_name;
+-----------+-----------+
| c_name    | 最高分    |
+-----------+-----------+
| 计算机    |        98 |
| 英语      |        94 |
| 中文      |        95 |
+-----------+-----------+
3 rows in set (0.05 sec)

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

mysql> select c_name,grade from score s join student st on s.stu_id=st.id where st.name='
广昌';
+-----------+-------+
| c_name    | grade |
+-----------+-------+
| 计算机    |    70 |
| 英语      |    92 |
+-----------+-------+
2 rows in set (0.00 sec)

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

mysql> select st.*,s.c_name,s.grade from student st left join score s on st.id=s.stu_id;
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| id  | name      | sex  | birth | department   | address            | c_name    | grade |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| 901 | 张三丰    | 男   |  2002 | 计算机系     | 北京市海淀区       | 英语      |    80 |
| 901 | 张三丰    | 男   |  2002 | 计算机系     | 北京市海淀区       | 计算机    |    98 |
| 902 | 周全有    | 男   |  2000 | 中文系       | 北京市昌平区       | 中文      |    88 |
| 902 | 周全有    | 男   |  2000 | 中文系       | 北京市昌平区       | 计算机    |    65 |
| 903 | 张思维    | 女   |  2003 | 中文系       | 湖南省永州市       | 中文      |    95 |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省皋新市       | 英语      |    92 |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省皋新市       | 计算机    |    70 |
| 905 | 王翰      | 男   |  2004 | 英语系       | 福建省厦门市       | 英语      |    94 |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       | 英语      |    83 |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       | 计算机    |    49 |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
10 rows in set (0.00 sec)

(9)计算每个学生的总成绩

mysql> select st.id,st.name,sum(grade) as '总成绩' from student st join score s on st.id=s.stu_id group by st.id,st.name;
+-----+-----------+-----------+
| id  | name      | 总成绩    |
+-----+-----------+-----------+
| 901 | 张三丰    |       178 |
| 902 | 周全有    |       153 |
| 903 | 张思维    |        95 |
| 904 | 李广昌    |       162 |
| 905 | 王翰      |        94 |
| 906 | 王心凌    |       132 |
+-----+-----------+-----------+
6 rows in set (0.00 sec)

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

mysql> select c_name,avg(grade) '平均成绩' from score group by c_name;
+-----------+--------------+
| c_name    | 平均成绩     |
+-----------+--------------+
| 计算机    |      70.5000 |
| 英语      |      87.2500 |
| 中文      |      91.5000 |
+-----------+--------------+
3 rows in set (0.00 sec)

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

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

(12)将计算机考试成绩按从高到低进行排序  两个方法

mysql> select * from score where c_name='计算机' order by grade desc;
+----+--------+-----------+-------+
| id | stu_id | c_name    | grade |
+----+--------+-----------+-------+
|  1 |    901 | 计算机    |    98 |
|  6 |    904 | 计算机    |    70 |
|  3 |    902 | 计算机    |    65 |
|  9 |    906 | 计算机    |    49 |
+----+--------+-----------+-------+
4 rows in set (0.00 sec)mysql> select * from student st join score s on st.id=s.stu_id where s.c_name='计算机' order by grade desc;
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
| id  | name      | sex  | birth | department   | address            | id | stu_id | c_name    | grade |
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
| 901 | 张三丰    | 男   |  2002 | 计算机系     | 北京市海淀区       |  1 |    901 | 计算机    |    98 |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省皋新市       |  6 |    904 | 计算机    |    70 |
| 902 | 周全有    | 男   |  2000 | 中文系       | 北京市昌平区       |  3 |    902 | 计算机    |    65 |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       |  9 |    906 | 计算机    |    49 |
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
4 rows in set (0.00 sec)

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

mysql> select id from student union select stu_id from score;
+-----+
| id  |
+-----+
| 901 |
| 902 |
| 903 |
| 904 |
| 905 |
| 906 |
+-----+
6 rows in set (0.00 sec)

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

mysql> select name,department,c_name,grade from student st join score s on st.id=s.stu_id where st.name like
'张%' or st.name like '王%';
+-----------+--------------+-----------+-------+
| name      | department   | c_name    | grade |
+-----------+--------------+-----------+-------+
| 张三丰    | 计算机系     | 计算机    |    98 |
| 张三丰    | 计算机系     | 英语      |    80 |
| 张思维    | 中文系       | 中文      |    95 |
| 王翰      | 英语系       | 英语      |    94 |
| 王心凌    | 计算机系     | 计算机    |    49 |
| 王心凌    | 计算机系     | 英语      |    83 |
+-----------+--------------+-----------+-------+
6 rows in set (0.00 sec)

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

mysql> select name,2025-birth '年龄' ,department,c_name,grade from student st join score s on st.id=s.stu_id
where address like '湖南省%';
+-----------+--------+--------------+-----------+-------+
| name      | 年龄   | department   | c_name    | grade |
+-----------+--------+--------------+-----------+-------+
| 张思维    |     22 | 中文系       | 中文      |    95 |
| 王心凌    |     27 | 计算机系     | 计算机    |    49 |
| 王心凌    |     27 | 计算机系     | 英语      |    83 |
+-----------+--------+--------------+-----------+-------+
3 rows in set (0.00 sec)mysql> select name,(year(now())-birth) age ,department,c_name,grade from student sc join score s on sc.id=s.stu_id where address like '湖南省%';
+-----------+------+--------------+-----------+-------+
| name      | age  | department   | c_name    | grade |
+-----------+------+--------------+-----------+-------+
| 张思维    |   22 | 中文系       | 中文      |    95 |
| 王心凌    |   27 | 计算机系     | 计算机    |    49 |
| 王心凌    |   27 | 计算机系     | 英语      |    83 |
+-----------+------+--------------+-----------+-------+
3 rows in set (0.00 sec)

http://www.dtcms.com/a/520212.html

相关文章:

  • Harbor VS Hadess,开源制品管理工具一文详细对比分析
  • 查找企业信息的网站哪个好广州网站外包
  • 基于3D激光点云的障碍物检测与跟踪---(3)基于匈牙利算法的障碍物跟踪
  • 虚拟化技术实践指南:KVM 与 VMware ESXi 部署全流程
  • 你的地图,你做主!视频汇聚平台EasyCVR解锁多源地图自由,打造监控“全域一张图”
  • Prometheus(四)—— Alertmanager完整部署指南:邮件+钉钉告警全流程落地
  • Fakebook.
  • (11)(2.1.7) FETtec OneWire ESCs
  • 红⿊树实现
  • HTML DOM 简介
  • 介绍东莞网站建设的ppt济南网站建设方案咨询
  • wordpress仿站入门wap网站不流行
  • Andrej Karpathy《Neural Networks: Zero to Hero》:从反向传播到GPT的实战课程
  • 舆情监测的技术内核:Infoseek 如何用分布式架构与多模态技术实现全网捕捉?
  • 影石Insta360发展史:从深圳公寓到全球影像创新标杆
  • 心理学网站的建设网站建设公司怀化
  • 欧姆龙plc内置 EIP 口实现 TCP SOCKET 通讯
  • 渲染相关(Markdown、ByteMD、ReactMarkdown)
  • 安庆信德建设咨询有限公司网站wordpress商城建站
  • esp8266初始化流程
  • SymPy矩阵到NumPy数组转换的深度解析:解决lambdify广播陷阱
  • ClickHouse迁移Starrocks脚本工具
  • LeeCode 74. 搜索二维矩阵
  • 网站建设报价单wordpress type参数
  • 长沙网站建设与维护樟木头镇仿做网站
  • Pandas DataFrame:深入理解数据分析的利器
  • Python嵌入(绿色免安装)版:解决安装第三方包后仍无法使用问题
  • 鸿蒙:将Resource类型的image转成 image.PixelMap 类型
  • 如何创建自己的网站平台网站项目建设措施
  • 网站论坛制作滕州手机网站建设案例