第十七周作业

1、 导入hellodb.sql生成数据库

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

1、导入hellodb.sql生成数据库
[root@centos7 ~]# mysql -uroot -pliucan123  show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| log                |
| mysql              |
| performance_schema |
| pid                |
| sys                |
+--------------------+
7 rows in set (0.00 sec)
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄 
mysql> use hellodb
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 |
+--------------+-----+
7 rows in set (0.00 sec)
(2) 以ClassID为分组依据,显示每组的平均年龄
mysql> select ClassID,AVG(Age) from students group by ClassID;
+---------+----------+
| ClassID | AVG(Age) |
+---------+----------+
|    NULL |  63.5000 |
|       1 |  20.5000 |
|       2 |  36.0000 |
|       3 |  20.2500 |
|       4 |  24.7500 |
|       5 |  46.0000 |
|       6 |  20.7500 |
|       7 |  19.6667 |
+---------+----------+
8 rows in set (0.00 sec)
(3) 显示第2题中平均年龄大于30的分组及平均年龄
mysql> select ClassID,AVG(Age) from students group by ClassID having AVG(age)>30;
+---------+----------+
| ClassID | AVG(Age) |
+---------+----------+
|    NULL |  63.5000 |
|       2 |  36.0000 |
|       5 |  46.0000 |
+---------+----------+
3 rows in set (0.00 sec)
(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 |
+-------+-------------+-----+--------+---------+-----------+

2、数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql

mysql> grant all on *.* to 'magedu'@'192.168.1.%' identified by 'magedu2020' with grant option
mysql> show grants for 'magedu'@'192.168.1.%';
+-------------------------------------------------------------------------+
| Grants for [email protected].%                                           |
+-------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'magedu'@'192.168.1.%' WITH GRANT OPTION |
+-------------------------------------------------------------------------+
1 row in set (0.00 sec)

你可能感兴趣的:(第十七周作业)