0
点赞
收藏
分享

微信扫一扫

第11周练习

1、 导入hellodb.sql生成数据库
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄

mysql> select * from students where Age > 25 and Gender='M';
+-------+--------------+-----+--------+---------+-----------+
| 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 |
|    24 | Xu Xian      |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng  | 100 | M      |    NULL |      NULL |
+-------+--------------+-----+--------+---------+-----------+
7 rows in set (0.00 sec)

(2) 以ClassID为分组依据,显示每组的平均年龄

mysql> select ClassID,count(*) 数量,avg(age) as 平均年龄 from students group by classid;
+---------+--------+--------------+
| ClassID | 数量   | 平均年龄     |
+---------+--------+--------------+
|       2 |      3 |      36.0000 |
|       1 |      4 |      20.5000 |
|       4 |      4 |      24.7500 |
|       3 |      4 |      20.2500 |
|       5 |      1 |      46.0000 |
|       7 |      3 |      19.6667 |
|       6 |      4 |      20.7500 |
|    NULL |      2 |      63.5000 |
+---------+--------+--------------+
8 rows in set (0.00 sec)

(3) 显示第2题中平均年龄大于30的分组及平均年龄

mysql> select ClassID,count(*) 数量,avg(age) as 平均年龄 from students group by classid having 平均年龄 > 30;
+---------+--------+--------------+
| ClassID | 数量   | 平均年龄     |
+---------+--------+--------------+
|       2 |      3 |      36.0000 |
|       5 |      1 |      46.0000 |
|    NULL |      2 |      63.5000 |
+---------+--------+--------------+
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 |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)

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

MariaDB [(none)]> grant all on *.* to magedu@'192.168.1.%' identified by 'magedu';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show grants for magedu@'192.168.1.%';
+--------------------------------------------------------------------------------------------------------------------------+
| Grants for magedu@192.168.1.%    |
+--------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'magedu'@'192.168.1.%' IDENTIFIED BY PASSWORD '*6B8CCC83799A26CD19D7AD9AEEADBCD30D8A8664' |
+--------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
举报

相关推荐

0 条评论