1展箱、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫(kù)
(1) 在students表中,查詢(xún)年齡大于25歲促王,且為男性的同學(xué)的名字和年齡
'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為分組依據(jù)瞒大,顯示每組的平均年齡
'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開(kāi)頭的名字的同學(xué)的信息
'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系洛、數(shù)據(jù)庫(kù)授權(quán)magedu用戶(hù)俊性,允許192.168.1.0/24網(wǎng)段可以連接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]'>