第十一周作业
1、 导入hellodb.sql生成数据库
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
# students表
mysql> 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 |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
# 查询结果
mysql[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 |
+--------------+-----+
(2) 以ClassID为分组依据,显示每组的平均年龄
mysql[hellodb]> select classid 班级,avg(age) 平均年龄 from students group by classid;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 2 | 36.0000 |
| 1 | 20.5000 |
| 4 | 24.7500 |
| 3 | 20.2500 |
| 5 | 46.0000 |
| 7 | 19.6667 |
| 6 | 20.7500 |
| NULL | 63.5000 |
+--------+--------------+
8 rows in set (0.00 sec)
mysql[hellodb]> select classid 班级,avg(age) 平均年龄 from students where classid is not null group by classid;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 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.00 sec)
mysql[hellodb]> select classid 班级,avg(age) 平均年龄 from students group by classid having classid is not null;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 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.00 sec)
(3) 显示第2题中平均年龄大于30的分组及平均年龄
mysql[hellodb]> select classid 班级,avg(age) 平均年龄 from students where classid is not null group by classid having 平均年龄 > 30;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 2 | 36.0000 |
| 5 | 46.0000 |
+--------+--------------+
2 rows in set (0.00 sec)
mysql[hellodb]> select classid 班级,avg(age) 平均年龄 from students group by classid having classid is not null and 平均年龄 > 30;
+--------+--------------+
| 班级 | 平均年龄 |
+--------+--------------+
| 2 | 36.0000 |
| 5 | 46.0000 |
+--------+--------------+
2 rows in set (0.00 sec)
(4) 显示以L开头的名字的同学的信息
mysql[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 |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
2、数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql
mysql> create user magedu@'192.168.1.%' identified by 'time9818';
#查看指定用户获得的授权
mysql> show grants for 'magedu'@'192.168.1.%';
+----------------------------------------------+
| Grants for magedu@192.168.1.% |
+----------------------------------------------+
| GRANT USAGE ON *.* TO `magedu`@`192.168.1.%` |
+----------------------------------------------+
1 row in set (0.00 sec)
#刷新权限
mysql> flush privileges;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构