第十一周作業(yè)

1、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫

(1) 在students表中,查詢年齡大于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開頭的名字的同學(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ù)庫授權(quán)magedu用戶,允許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]'> 

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、導(dǎo)入hellodb.sql生成數(shù)據(jù)庫 [root@centos7 ~]# mysql -p < hellodb...
    Gustav_man閱讀 133評(píng)論 0 0
  • 1、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫 (1) 在students表中,查詢年齡大于25歲,且為男性的同學(xué)的名...
    馬暉閱讀 221評(píng)論 0 0
  • 1、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫 (1) 在students表中,查詢年齡大于25歲,且為男性的同學(xué)的名...
    沐熙一葉_Leaf閱讀 230評(píng)論 1 0
  • 1、導(dǎo)入hellodb.sql生成數(shù)據(jù)庫 (1) 在students表中,查詢年齡大于25歲,且為男性的同學(xué)的名字...
    歸來兮_007閱讀 340評(píng)論 0 0
  • 1、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫 (1) 在students表中,查詢年齡大于25歲,且為男性的同學(xué)的名...
    毅_閱讀 210評(píng)論 0 0

友情鏈接更多精彩內(nèi)容