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


3. 导入hellodb.sql生成数据库
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
(2) 以ClassID为分组依据,显示每组的平均年龄
(3) 显示第2题中平均年龄大于30的分组及平均年龄
(4) 显示以L开头的名字的同学的信息

导入hellodb.sql生成数据库
[root@CentOS8 ~]# mysql -u root -pMmagedu0! < /root/hellodb_innodb.sql

[root@CentOS8 ~]# systemctl restart mysqld

[root@CentOS8 ~]# ss -ntl | grep 3306
LISTEN 0 128 *:3306 *:*
LISTEN 0 70 *:33060 *:*

[root@CentOS8 ~]# mysql -uroot -pMmagedu0!

(root@localhost) [(none)]> show databases;
+——————–+
| Database |
+——————–+
| db1 |
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| sys |
| zabbix |
+——————–+
7 rows in set (0.00 sec)

(root@localhost) [(none)]> use hellodb;

(root@localhost) [hellodb]> show tables;
+——————-+
| Tables_in_hellodb |
+——————-+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+——————-+
7 rows in set (0.00 sec)

(root@localhost) [hellodb]> select * from students;
+——-+—————+—–+——–+———+———–+
| 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 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |

(root@localhost) [hellodb]> exit

 

(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
(root@localhost) [(none)]> use hellodb;
(root@localhost) [hellodb]> 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 |
+————–+—–+
7 rows in set (0.00 sec)

 

(2) 以ClassID为分组依据,显示每组的平均年龄
(root@localhost) [hellodb]> 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 |
+———+———-+
7 rows in set (0.03 sec)

 

 

(3) 显示第2题中平均年龄大于30的分组及平均年龄
(root@localhost) [hellodb]> select classid,avg(age) as avgage from students where classid is not null group by classid having avgage>30;
+———+———+
| classid | avgage |
+———+———+
| 2 | 36.0000 |
| 5 | 46.0000 |
+———+———+

 

 

(4) 显示以L开头的名字的同学的信息
(root@localhost) [hellodb]> 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 |
+——-+————-+—–+——–+———+———–+

 

原创文章,作者:745907710,如若转载,请注明出处:https://blog.ytso.com/277977.html

(0)
上一篇 2022年7月31日
下一篇 2022年7月31日

相关推荐

发表回复

登录后才能评论