第十七周作業(yè)

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

(1) 在students表中,查詢年齡大于25歲整葡,且為男性的同學的名字和年齡
(2) 以ClassID為分組依據(jù),顯示每組的平均年齡
(3) 顯示第2題中平均年齡大于30的分組及平均年齡
(4) 顯示以L開頭的名字的同學的信息

#導(dǎo)入hellodb.sql
[root@centos8 ~]# mysql -uroot -pyyang <hellodb.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

#檢查導(dǎo)入數(shù)據(jù)庫
[root@centos8 ~]# mysql -uroot -pyyang 
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
7 rows in set (0.01 sec)

# 在students表中鲫懒,查詢年齡大于25歲,且為男性的同學的名字和年齡
mysql> select name,age from students where age > 25 and gender = "M" ;
+--------------+-----+
| 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.01 sec)

#以ClassID為分組依據(jù)刽辙,顯示每組的平均年齡
mysql> select classid,avg(age) as 平均年齡 from students group by classid;
+---------+--------------+
| classid | 平均年齡     |
+---------+--------------+
|    NULL |      63.5000 |
|       1 |      20.5000 |
|       2 |      36.0000 |
|       3 |      20.2500 |
|       4 |      24.7500 |
|       5 |      46.0000 |
|       6 |      20.7500 |
|       7 |      19.6667 |
+---------+--------------+
8 rows in set (0.00 sec)

#顯示第2題中平均年齡大于30的分組及平均年齡
mysql> select classid,avg(age) as 平均年齡 from students group by classid having 平均年齡 > 30;
+---------+--------------+
| classid | 平均年齡     |
+---------+--------------+
|    NULL |      63.5000 |
|       2 |      36.0000 |
|       5 |      46.0000 |
+---------+--------------+
3 rows in set (0.03 sec)

#顯示以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窥岩、數(shù)據(jù)庫授權(quán)magedu用戶,允許192.168.1.0/24網(wǎng)段可以連接mysql

[root@centos8 ~]# mysql -uroot -pyyang 
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all on *.* to 'magedu'@'192.168.1.%' IDENTIFIED BY '12345678';
Query OK, 0 rows affected (0.05 sec)
mysql> select user,host from user;
+---------------+-------------+
| user          | host        |
+---------------+-------------+
| magedu        | 192.168.1.% |
| mysql.session | localhost   |
| mysql.sys     | localhost   |
| root          | localhost   |
+---------------+-------------+
4 rows in set (0.00 sec)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末宰缤,一起剝皮案震驚了整個濱河市颂翼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌慨灭,老刑警劉巖朦乏,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異氧骤,居然都是意外死亡呻疹,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進店門筹陵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刽锤,“玉大人,你說我怎么就攤上這事朦佩〔⑺迹” “怎么了?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵吕粗,是天一觀的道長。 經(jīng)常有香客問我旭愧,道長颅筋,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任输枯,我火速辦了婚禮议泵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘桃熄。我一直安慰自己先口,他們只是感情好,可當我...
    茶點故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布瞳收。 她就那樣靜靜地躺著碉京,像睡著了一般。 火紅的嫁衣襯著肌膚如雪螟深。 梳的紋絲不亂的頭發(fā)上谐宙,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天,我揣著相機與錄音界弧,去河邊找鬼凡蜻。 笑死搭综,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的划栓。 我是一名探鬼主播兑巾,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼忠荞!你這毒婦竟也來了蒋歌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤钻洒,失蹤者是張志新(化名)和其女友劉穎奋姿,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體素标,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡称诗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了头遭。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片寓免。...
    茶點故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖计维,靈堂內(nèi)的尸體忽然破棺而出袜香,到底是詐尸還是另有隱情,我是刑警寧澤鲫惶,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布蜈首,位于F島的核電站,受9級特大地震影響欠母,放射性物質(zhì)發(fā)生泄漏欢策。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一赏淌、第九天 我趴在偏房一處隱蔽的房頂上張望踩寇。 院中可真熱鬧,春花似錦六水、人聲如沸俺孙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽睛榄。三九已至,卻和暖如春想帅,著一層夾襖步出監(jiān)牢的瞬間懈费,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工博脑, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留憎乙,地道東北人票罐。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像泞边,于是被迫代替她去往敵國和親该押。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內(nèi)容