第11周作業
導入hellodb.sql生成數據庫
MariaDB [mysql]> source /root/hellodb_innodb.sql
(1) 在students表中,查詢年齡大于25歲,且為男性的同學的名字和年齡
MariaDB [hellodb]> select Name,Gender,Age from students where Age>25 and Gender='M';
+--------------+--------+-----+
| Name | Gender | Age |
+--------------+--------+-----+
| Xie Yanke | M | 53 |
| Ding Dian | M | 32 |
| Yu Yutong | M | 26 |
| Shi Qing | M | 46 |
| Tian Boguang | M | 33 |
| Xu Xian | M | 27 |
| Sun Dasheng | M | 100 |
+--------------+--------+-----+
(2) 以ClassID為分組依據,顯示每組的平均年齡
MariaDB [hellodb]> 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 |
+---------+----------+
(3) 顯示第2題中平均年齡大于30的分組及平均年齡
MariaDB [hellodb]> 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 |
+---------+----------+
(4) 顯示以L開頭的名字的同學的信息
MariaDB [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 |
+-------+-------------+-----+--------+---------+-----------+
數據庫授權magedu用戶,允許192.168.1.0/24網段可以連接mysql
GRANT ALL PRIVILEGES ON hellodb.* TO magedu@'192.168.1.%' IDENTIFIED BY 'centos';
FLUSH PRIVILEGES;
浙公網安備 33010602011771號