1、 导入hellodb.sql生成数据库
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
'07:39:59(root@localhost) [hellodb]'> select name,age from students where gender='M' and age > 25;
+--------------+-----+
| 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.11 sec)
(2) 以ClassID为分组依据,显示每组的平均年龄
'07:42:40(root@localhost) [hellodb]'> select classid,avg(age) from students 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 |
| NULL | 49.7500 |
+---------+----------+
8 rows in set (0.00 sec)
(3) 显示第2题中平均年龄大于30的分组及平均年龄
'07:55:32(root@localhost) [hellodb]'> select cid,avg from (select classid as cid,avg(age) as avg from students group by classid) as avgtb where avg > 30;
+------+---------+
| cid | avg |
+------+---------+
| 2 | 36.0000 |
| 5 | 46.0000 |
| NULL | 49.7500 |
+------+---------+
3 rows in set (0.00 sec)
(4) 显示以L开头的名字的同学的信息
'08:00:19(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 |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
2、数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql
'08:01:07(root@localhost) [hellodb]'> create user 'magedu'@'192.168.1.%' identified by '123456';
Query OK, 0 rows affected (0.13 sec)
'08:06:19(root@localhost) [hellodb]'> grant select on hellodb.* to magedu@'192.168.156.%';
Query OK, 0 rows affected (0.00 sec)
'08:07:33(root@localhost) [hellodb]'> show processlist;
+----+-----------------+-----------------------+---------+---------+------+------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------------+---------+---------+------+------------------------+------------------+
| 5 | event_scheduler | localhost | NULL | Daemon | 1747 | Waiting on empty queue | NULL |
| 8 | root | localhost | hellodb | Query | 0 | init | show processlist |
| 11 | magedu | 192.168.156.204:54828 | NULL | Sleep | 41 | | NULL |
+----+-----------------+-----------------------+---------+---------+------+------------------------+------------------+
3 rows in set (0.00 sec)
'08:08:50(root@localhost) [hellodb]'>