首页 > 数据库 >导入hellodb.sql生成数据库 ,完成下列操作

导入hellodb.sql生成数据库 ,完成下列操作

时间:2022-12-05 07:33:13浏览次数:64  
标签:ClassID hellodb students Age mysql 导入 sql NULL SELECT

导入hellodb.sql生成数据库 ,完成下列操作

目录

导入hellodb.sql

下载链接:https://wwi.lanzoup.com/ieK0q0htj9mh

[root@node1 ~]# mysql -uroot -p'Mysql@123' < /root/hellodb.sql
[root@node1 ~]# mysql -uroot -p'Mysql@123'
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| hellodb            |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> use hellodb;
mysql> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
7 rows in set (0.00 sec)

查询

1. 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
mysql> SELECT name,age FROM students WHERE age>25 AND gender = 'M';
+--------------+-----+
| name         | age |
+--------------+-----+
| Xie Yanke    |  53 |
| Ding Dian    |  32 |
| Yu Yutong    |  26 |
| Shi Qing     |  46 |
| Tian Boguang |  33 |
| Xu Xian      |  27 |
| Sun Dasheng  | 100 |
+--------------+-----+
2.以ClassID为分组依据,显示每组的平均年龄
mysql> SELECT ClassID,AVG(Age) FROM students WHERE ClassID IS NOT NULL GROUP BY ClassID; 
+---------+----------+
| ClassID | AVG(Age) |
+---------+----------+
|       2 |  36.0000 |
|       1 |  20.5000 |
|       4 |  24.7500 |
|       3 |  20.2500 |
|       5 |  46.0000 |
|       7 |  19.6667 |
|       6 |  20.7500 |
+---------+----------+
3.显示第2题中平均年龄大于30的分组及平均年龄
mysql> SELECT ClassID,AVG(Age) FROM students WHERE ClassID IS NOT NULL GROUP BY ClassID HAVING AVG(Age)>30;
+---------+----------+
| ClassID | AVG(Age) |
+---------+----------+
|       2 |  36.0000 |
|       5 |  46.0000 |
+---------+----------+
4.显示以L开头的名字的同学的信息
mysql> SELECT * FROM students WHERE `Name` like "l%";
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name        | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
|     8 | Lin Daiyu   |  17 | F      |       7 |      NULL |
|    14 | Lu Wushuang |  17 | F      |       3 |      NULL |
|    17 | Lin Chong   |  25 | M      |       4 |      NULL |
+-------+-------------+-----+--------+---------+-----------+
5.显示TeacherID非空的同学的相关信息
mysql> SELECT * FROM students WHERE TeacherID IS NOT NULL;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name        | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu |  22 | M      |       2 |         3 |
|     2 | Shi Potian  |  22 | M      |       1 |         7 |
|     3 | Xie Yanke   |  53 | M      |       2 |        16 |
|     4 | Ding Dian   |  32 | M      |       4 |         4 |
|     5 | Yu Yutong   |  26 | M      |       3 |         1 |
+-------+-------------+-----+--------+---------+-----------+
6.以年龄排序后,显示年龄最大的前10位同学的信息
mysql> SELECT * FROM students ORDER BY age DESC LIMIT 10;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
+-------+---------------+-----+--------+---------+-----------+
7.查询年龄大于等于20岁,小于等于25岁的同学的信息
 mysql> SELECT * FROM students WHERE Age>=20 AND Age<=25;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
+-------+---------------+-----+--------+---------+-----------+

或

mysql> SELECT * FROM students WHERE Age BETWEEN 20 AND 25;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
+-------+---------------+-----+--------+---------+-----------+

8.以ClassID分组,显示每班的同学的人数
mysql> SELECT ClassID,COUNT(StuID) FROM students GROUP BY ClassID;
+---------+--------------+
| ClassID | COUNT(StuID) |
+---------+--------------+
|       2 |            3 |
|       1 |            4 |
|       4 |            4 |
|       3 |            4 |
|       5 |            1 |
|       7 |            3 |
|       6 |            4 |
|    NULL |            2 |
+---------+--------------+
9.以Gender分组,显示其年龄之和
mysql> SELECT Gender,SUM(Age) FROM students GROUP BY Gender ;
+--------+----------+
| Gender | SUM(Age) |
+--------+----------+
| M      |      495 |
| F      |      190 |
+--------+----------+
10.以ClassID分组,显示其平均年龄大于25的班级
mysql> SELECT ClassID,AVG(Age) FROM students GROUP BY ClassID HAVING AVG(Age)>25;
+---------+----------+
| ClassID | AVG(Age) |
+---------+----------+
|       2 |  36.0000 |
|       5 |  46.0000 |
|    NULL |  63.5000 |
+---------+----------+
11.以Gender分组,显示各组中年龄大于25的学员的年龄之和
mysql> SELECT Gender,SUM(Age) FROM students WHERE Age>25 GROUP BY Gender;
+--------+----------+
| Gender | SUM(Age) |
+--------+----------+
| M      |      317 |
+--------+----------+
12.显示前5位同学的姓名、课程及成绩
mysql> SELECT name,Course,Score FROM students st INNER JOIN scores sc ON st.StuID=sc.StuID INNER JOIN courses co ON sc.CourseID=co.CourseID LIMIT 5;
+-------------+----------------+-------+
| name        | Course         | Score |
+-------------+----------------+-------+
| Shi Zhongyu | Kuihua Baodian |    77 |
| Shi Zhongyu | Weituo Zhang   |    93 |
| Shi Potian  | Kuihua Baodian |    47 |
| Shi Potian  | Daiyu Zanghua  |    97 |
| Xie Yanke   | Kuihua Baodian |    88 |
+-------------+----------------+-------+
13.显示其成绩高于80的同学的名称及课程
mysql> SELECT st.`Name`,sc.Score,cl.Class FROM students st INNER JOIN scores sc ON sc.StuID=st.StuID INNER JOIN classes cl ON st.ClassID=cl.ClassID WHERE sc.Score>80;
+-------------+-------+----------------+
| Name        | Score | Class          |
+-------------+-------+----------------+
| Shi Zhongyu |    93 | Emei Pai       |
| Shi Potian  |    97 | Shaolin Pai    |
| Xie Yanke   |    88 | Emei Pai       |
| Ding Dian   |    89 | Wudang Pai     |
| Shi Qing    |    96 | Riyue Shenjiao |
| Xi Ren      |    86 | QingCheng Pai  |
| Xi Ren      |    83 | QingCheng Pai  |
| Lin Daiyu   |    93 | Ming Jiao      |
+-------------+-------+----------------+
14.取每位同学各门课的平均成绩,显示成绩前三名的同学的姓名和平均成绩
mysql> SELECT st.name,avg(score) 平均成绩 FROM students st INNER JOIN scores sc on st.StuID=sc.StuID GROUP BY st.`Name` ORDER BY 平均成绩 DESC LIMIT 3;
+-------------+--------------+
| name        | 平均成绩      |
+-------------+--------------+
| Shi Qing    |      96.0000 |
| Shi Zhongyu |      85.0000 |
| Xi Ren      |      84.5000 |
+-------------+--------------+
15.显示每门课程课程名称及学习了这门课的同学的个数
mysql> SELECT Course,COUNT(StuID) FROM scores sc INNER JOIN courses co ON sc.CourseID=co.CourseID GROUP BY Course;
+----------------+--------------+
| Course         | COUNT(StuID) |
+----------------+--------------+
| Kuihua Baodian |            4 |
| Weituo Zhang   |            2 |
| Daiyu Zanghua  |            2 |
| Hamo Gong      |            3 |
| Dagou Bangfa   |            2 |
| Taiji Quan     |            1 |
| Jinshe Jianfa  |            1 |
+----------------+--------------+
16.显示其年龄大于平均年龄的同学的名字
mysql> SELECT `Name` FROM students st WHERE st.Age>(SELECT AVG(Age) FROM students);
+--------------+
| Name         |
+--------------+
| Xie Yanke    |
| Ding Dian    |
| Shi Qing     |
| Tian Boguang |
| Sun Dasheng  |
+--------------+
17.显示其学习的课程为第1、2,4或第7门课的同学的名字
mysql> SELECT`Name`,CourseID FROM students st INNER JOIN scores sc ON st.StuID=sc.StuID WHERE sc.CourseID IN (1,2,4) OR sc.CourseID = 7;
+-------------+----------+
| Name        | CourseID |
+-------------+----------+
| Shi Zhongyu |        2 |
| Shi Potian  |        2 |
| Xie Yanke   |        2 |
| Ding Dian   |        2 |
| Yu Yutong   |        1 |
| Yu Yutong   |        7 |
| Shi Qing    |        1 |
| Xi Ren      |        1 |
| Xi Ren      |        7 |
| Lin Daiyu   |        4 |
+-------------+----------+
18.显示其成员数最少为3个的班级的同学中年龄大于同班同学平均年龄的同学
mysql> SELECT ClassID,COUNT(StuID),AVG(Age) FROM students GROUP BY ClassID HAVING COUNT(StuID)>=3;
+---------+--------------+----------+
| ClassID | COUNT(StuID) | AVG(Age) |
+---------+--------------+----------+
|       2 |            3 |  36.0000 |
|       1 |            4 |  20.5000 |
|       4 |            4 |  24.7500 |
|       3 |            4 |  20.2500 |
|       7 |            3 |  19.6667 |
|       6 |            4 |  20.7500 |
+---------+--------------+----------+
19.统计各班级中年龄大于全校同学平均年龄的同学
mysql> SELECT * FROM students WHERE Age > (SELECT AVG(Age) FROM students WHERE ClassID IS NOT NULL) AND ClassID IS NOT NULL;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name         | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
|     3 | Xie Yanke    |  53 | M      |       2 |        16 |
|     4 | Ding Dian    |  32 | M      |       4 |         4 |
|     5 | Yu Yutong    |  26 | M      |       3 |         1 |
|     6 | Shi Qing     |  46 | M      |       5 |      NULL |
|    13 | Tian Boguang |  33 | M      |       2 |      NULL |
|    17 | Lin Chong    |  25 | M      |       4 |      NULL |
+-------+--------------+-----+--------+---------+-----------+

标签:ClassID,hellodb,students,Age,mysql,导入,sql,NULL,SELECT
From: https://www.cnblogs.com/feifa/p/16951362.html

相关文章

  • sqlserver 中len和DataLength的区别
    https://zhuanlan.zhihu.com/p/342051927LEN()函数返回文本字段中值的长度。DataLength()函数返回文本字段中值的字符长度。区别在于:len()只返回字符数,一个汉字代表一个......
  • mysql盲注
    盲注是在没有回显的情况下使用的注入方式,根据盲注方式的不同分为布尔盲注和时间盲注一、布尔盲注布尔盲注前提条件是存在注入点并且条件成功和失败页面特征不一致,基于布......
  • MySQL和SQLserver中group by的区别
    https://blog.csdn.net/weixin_48720080/article/details/126289174力扣中做了一道题,groupby可以在mysql中用,不能在mysqlserver中用,因为groupby后面没有select中的字段......
  • 012.预防SQL注入攻击
    1.什么是SQL注入攻击  2.Mybatis的俩种传值方式  3.Mybatis的俩种传值方式的使用场景3.1goods.xml<selectid="selectByTitle"parameterType="java.uti......
  • 导入博客等渠道文章到公众号并进行格式布局-详细教程
    〇、参考链接1、常用的Markdown软件对比https://blog.csdn.net/best_luxi/article/details/121786709一、打开微小宝1、下载地址https://www.wxb.com/2、注册登录 ......
  • 忘记mysql本地root的登录密码该怎么办??
    解决过程:1、编辑/etc/my.cnf在[mysqld]配置部分添加一行skip-grant-tables2、保存后重启mysql[root@localhostetc]#servicemysqldrestartShuttingdownMySQL.......
  • MySQL MHA
    一、MHA概述MHA(MasterHighAvailability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件。MHA的出现就是解决MySQL单点故障的问题。MySQL故障切换过程中,MHA能做......
  • 在C#中使用Irony实现SQL语句的解析
    https://sunnycoding.cn/2019/07/11/sql-parser-with-irony-in-csharp/ 本文结构定义语法语法测试语句解析获得解析结果总结在上一篇博文中,我介绍了LOGO语言的C......
  • Mysql 权限操作
    --使用mysql库,查询host和用户名usemysql;selecthost,userFROMuser--创建一个用户(这样创建默认是%,表示任何ip都可以连接)createuser'zhang3(这里填写用户名)'IDEN......
  • MySQL 存储过程浅谈
    一、存储过程定义​存储过程(StoredProcedure):一组为了完成特定功能的SQL语句集,存储在数据库中,经过一次编译后不需要再次编译。二、存储过程特点1、可以完成复杂的判断和......