linux進階-MySQL數據庫相關練習

MySQL數據庫相關練習

文章目錄


在這裏插入圖片描述

大家好,我們又見面了!

實驗準備

[root@centos7~]# yum -y install MariaDB-server #安裝數據庫服務器端服務 ✅
-------------------------------------------------------------
[root@centos7~]# systemctl start mysqld #啓用數據庫服務 ✅
---------------------------------------------------------------
[root@centos7~]# mysql_secure_installation #對數據庫進行安全加固 ✅
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:   #輸入數據庫密碼 ✅
Re-enter new password:   #確認密碼 ✅
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@centos7~]# mysql -uroot -p < hellodb_innodb.sql
Enter password:
[root@centos7~]# mysql -uroot -p
Enter password: 
MariaDB [hellodb]> show databases; #列出庫文件 ✅
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
6 rows in set (0.00 sec)
-------------------------------------------------------------
MariaDB [(none)]> 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
MariaDB [hellodb]>
----------------------------------------------------------------
MariaDB [hellodb]> desc students; #正序排列學生表 ✅
+-----------+---------------------+------+-----+---------+----------------+
| Field     | Type                | Null | Key | Default | Extra          |
+-----------+---------------------+------+-----+---------+----------------+
| StuID     | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| Name      | varchar(50)         | NO   |     | NULL    |                |
| Age       | tinyint(3) unsigned | NO   |     | NULL    |                |
| Gender    | enum('F','M')       | NO   |     | NULL    |                |
| ClassID   | tinyint(3) unsigned | YES  |     | NULL    |                |
| TeacherID | int(10) unsigned    | YES  |     | NULL    |                |
+-----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

students表內容

MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)

courses表內容

MariaDB [hellodb]> select * from courses;
+----------+----------------+
| CourseID | Course         |
+----------+----------------+
|        1 | Hamo Gong      |
|        2 | Kuihua Baodian |
|        3 | Jinshe Jianfa  |
|        4 | Taiji Quan     |
|        5 | Daiyu Zanghua  |
|        6 | Weituo Zhang   |
|        7 | Dagou Bangfa   |
+----------+----------------+
7 rows in set (0.00 sec)

scores表內容

MariaDB [hellodb]> select * from scores;
+----+-------+----------+-------+
| ID | StuID | CourseID | Score |
+----+-------+----------+-------+
|  1 |     1 |        2 |    77 |
|  2 |     1 |        6 |    93 |
|  3 |     2 |        2 |    47 |
|  4 |     2 |        5 |    97 |
|  5 |     3 |        2 |    88 |
|  6 |     3 |        6 |    75 |
|  7 |     4 |        5 |    71 |
|  8 |     4 |        2 |    89 |
|  9 |     5 |        1 |    39 |
| 10 |     5 |        7 |    63 |
| 11 |     6 |        1 |    96 |
| 12 |     7 |        1 |    86 |
| 13 |     7 |        7 |    83 |
| 14 |     8 |        4 |    57 |
| 15 |     8 |        3 |    93 |
+----+-------+----------+-------+
15 rows in set (0.00 sec)

實驗案例

1.在學生表中,查詢年齡大於25歲,且爲男性的同學的名字和年齡:

MariaDB [hellodb]> select students.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.00 sec)

2.在學生表中,以ClassID爲分組依據,查詢顯示每組的平均年齡:

MariaDB [hellodb]> select ClassID, avg(Age) as avgAge from students group by ClassID;
+---------+---------+
| ClassID | avgAge  |
+---------+---------+
|    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)

3.顯示第2題中平均年齡大於30的分組及平均年齡:

MariaDB [hellodb]> select ClassID, avg(Age) as avgAge from students group by ClassID having avgAge > 30;
+---------+---------+
| ClassID | avgAge  |
+---------+---------+
|    NULL | 63.5000 |
|       2 | 36.0000 |
|       5 | 46.0000 |
+---------+---------+
3 rows in set (0.00 sec)

4.顯示以L開頭的名字的同學的信息

MariaDB [hellodb]> select * from students where name="L%";
Empty set (0.00 sec)

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 |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
---------------------------------------------------------------------
MariaDB [hellodb]> select * from students where Name rlike "^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)
---------------------------------------------------------------------
MariaDB [hellodb]> select * from students where Name regexp "^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)

5.顯示老師ID非空的同學的相關信息:

MariaDB [hellodb]> select * from students where TeacherID is noull;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name        | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu |  22 | M      |       2 |         3 |
|     2 | Shi Potian  |  22 | M      |       1 |         7 |
|     3 | Xie Yanke   |  53 | M      |       2 |        16 |
|     4 | Ding Dian   |  32 | M      |       4 |         4 |
|     5 | Yu Yutong   |  26 | M      |       3 |         1 |
+-------+-------------+-----+--------+---------+-----------+
5 rows in set (0.00 sec)

6.students表中,查詢以年齡排序後的數據,並且顯示年齡最大的前10位同學的信息

MariaDB [hellodb]> select * from students order by Age desc limit 10 ;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name         | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
|    25 | Sun Dasheng  | 100 | M      |    NULL |      NULL |
|     3 | Xie Yanke    |  53 | M      |       2 |        16 |
|     6 | Shi Qing     |  46 | M      |       5 |      NULL |
|    13 | Tian Boguang |  33 | M      |       2 |      NULL |
|     4 | Ding Dian    |  32 | M      |       4 |         4 |
|    24 | Xu Xian      |  27 | M      |    NULL |      NULL |
|     5 | Yu Yutong    |  26 | M      |       3 |         1 |
|    17 | Lin Chong    |  25 | M      |       4 |      NULL |
|    23 | Ma Chao      |  23 | M      |       4 |      NULL |
|    18 | Hua Rong     |  23 | M      |       7 |      NULL |
+-------+--------------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)

7.students表中,查詢年齡大於等於20歲,小於等於25歲的同學的信息

#7.1
MariaDB [hellodb]> select * from students where Age >=20 and Age <=25;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
+-------+---------------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)
---------------------------------------------------------------------
#7.2
MariaDB [hellodb]> select * from students where Age between 20 and 25;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
+-------+---------------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)
--------------------------------------------------------------------
#7.3
MariaDB [hellodb]> select * from students where Age in (20,21,22,23,24,25);
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
+-------+---------------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)

8.以ClassID分組,顯示每班的同學的人數:

MariaDB [hellodb]> select ClassID,count(*) as numbers from students group by ClassID;
+---------+---------+
| ClassID | numbers |
+---------+---------+
|    NULL |       2 |
|       1 |       4 |
|       2 |       3 |
|       3 |       4 |
|       4 |       4 |
|       5 |       1 |
|       6 |       4 |
|       7 |       3 |
+---------+---------+
8 rows in set (0.00 sec)

9.以性別分組,顯示其年齡之和:

MariaDB [hellodb]> select Gender ,sum(Age) as tatalAge from students group by Gender;
+--------+----------+
| Gender | tatalAge |
+--------+----------+
| F      |      190 |
| M      |      495 |
+--------+----------+
2 rows in set (0.00 sec)

10.以ClassID分組,顯示其平均年齡大於25的班級:

MariaDB [hellodb]> select ClassID,avg(Age) as avgAge from students group by ClassID having avgAge >25 ;
+---------+---------+
| ClassID | avgAge  |
+---------+---------+
|    NULL | 63.5000 |
|       2 | 36.0000 |
|       5 | 46.0000 |
+---------+---------+
3 rows in set (0.00 sec)

11.以Gender分組,顯示各組中年齡大於25的學者的年齡之和:

#11.1
MariaDB [hellodb]> select * from students where Age > 25;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name         | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
|     3 | Xie Yanke    |  53 | M      |       2 |        16 |
|     4 | Ding Dian    |  32 | M      |       4 |         4 |
|     5 | Yu Yutong    |  26 | M      |       3 |         1 |
|     6 | Shi Qing     |  46 | M      |       5 |      NULL |
|    13 | Tian Boguang |  33 | M      |       2 |      NULL |
|    24 | Xu Xian      |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng  | 100 | M      |    NULL |      NULL |
+-------+--------------+-----+--------+---------+-----------+
7 rows in set (0.00 sec)

#11.2
MariaDB [hellodb]> select Gender,sum(Age) as sumAge from students where Age > 25  group by Gender ;
+--------+--------+
| Gender | sumAge |
+--------+--------+
| M      |    317 |
+--------+--------+
1 row in set (0.00 sec)

12.顯示前5位同學的姓名,課程及成績

#12.1
MariaDB [hellodb]> select stu.StuID,stu.Name,sc.Score,co.Course from students as stu inner join scores as sc on stu.StuID=sc.StuID inner join courses as co on sc.CourseID=co.CourseID where stu.StuID < 5;
+-------+-------------+-------+----------------+
| StuID | Name        | Score | Course         |
+-------+-------------+-------+----------------+
|     1 | Shi Zhongyu |    77 | Kuihua Baodian |
|     1 | Shi Zhongyu |    93 | Weituo Zhang   |
|     2 | Shi Potian  |    47 | Kuihua Baodian |
|     2 | Shi Potian  |    97 | Daiyu Zanghua  |
|     3 | Xie Yanke   |    88 | Kuihua Baodian |
|     3 | Xie Yanke   |    75 | Weituo Zhang   |
|     4 | Ding Dian   |    71 | Daiyu Zanghua  |
|     4 | Ding Dian   |    89 | Kuihua Baodian |
+-------+-------------+-------+----------------+
8 rows in set (0.00 sec)
------------------------------------------------------------------------------------------------------
#12.2
MariaDB [hellodb]> select stu.StuID, stu.Name, co.Course, sc.Score from (select * from students where StuID <= 5) as stu inner join scores sc on stu.StuID = sc.StuID inner join courses co on sc.CourseID = co.CourseID; 
+-------+-------------+----------------+-------+
| StuID | Name        | Course         | Score |
+-------+-------------+----------------+-------+
|     1 | Shi Zhongyu | Kuihua Baodian |    77 |
|     1 | Shi Zhongyu | Weituo Zhang   |    93 |
|     2 | Shi Potian  | Kuihua Baodian |    47 |
|     2 | Shi Potian  | Daiyu Zanghua  |    97 |
|     3 | Xie Yanke   | Kuihua Baodian |    88 |
|     3 | Xie Yanke   | Weituo Zhang   |    75 |
|     4 | Ding Dian   | Daiyu Zanghua  |    71 |
|     4 | Ding Dian   | Kuihua Baodian |    89 |
|     5 | Yu Yutong   | Hamo Gong      |    39 |
|     5 | Yu Yutong   | Dagou Bangfa   |    63 |
+-------+-------------+----------------+-------+
10 rows in set (0.00 sec)

13.顯示其成績高於80的同學的名稱及課程

#13.1
MariaDB [hellodb]> select stu.StuID,sc.Score,stu.Name,co.Course from students as stu inner join scores as sc on stu.StuID=sc.StuID inner join courses as co on sc.CourseID=co.CourseID where sc.Score > 80;
+-------+-------+-------------+----------------+
| StuID | Score | Name        | Course         |
+-------+-------+-------------+----------------+
|     1 |    93 | Shi Zhongyu | Weituo Zhang   |
|     2 |    97 | Shi Potian  | Daiyu Zanghua  |
|     3 |    88 | Xie Yanke   | Kuihua Baodian |
|     4 |    89 | Ding Dian   | Kuihua Baodian |
|     6 |    96 | Shi Qing    | Hamo Gong      |
|     7 |    86 | Xi Ren      | Hamo Gong      |
|     7 |    83 | Xi Ren      | Dagou Bangfa   |
|     8 |    93 | Lin Daiyu   | Jinshe Jianfa  |
+-------+-------+-------------+----------------+
8 rows in set (0.00 sec)
-----------------------------------------------------------------------------------------------------
#13.2
MariaDB [hellodb]> select stu.StuID, stu.Name, co.Course, sc.Score from (select * from students where StuID <= 5) as stu inner join scores sc on stu.StuID = sc.StuID inner join courses co on sc.CourseID = co.CourseID;
+-------+-------------+----------------+-------+
| StuID | Name        | Course         | Score |
+-------+-------------+----------------+-------+
|     1 | Shi Zhongyu | Kuihua Baodian |    77 |
|     1 | Shi Zhongyu | Weituo Zhang   |    93 |
|     2 | Shi Potian  | Kuihua Baodian |    47 |
|     2 | Shi Potian  | Daiyu Zanghua  |    97 |
|     3 | Xie Yanke   | Kuihua Baodian |    88 |
|     3 | Xie Yanke   | Weituo Zhang   |    75 |
|     4 | Ding Dian   | Daiyu Zanghua  |    71 |
|     4 | Ding Dian   | Kuihua Baodian |    89 |
|     5 | Yu Yutong   | Hamo Gong      |    39 |
|     5 | Yu Yutong   | Dagou Bangfa   |    63 |
+-------+-------------+----------------+-------+
10 rows in set (0.00 sec)

14.取每位同學各門課的平均成績,顯示成績前三名的同學的姓名和平均成績

#14.1
MariaDB [hellodb]> select stsc.Name,avg(stsc.Score) as newsc from (select stu.StuID,stu.Name,sc.Score from students as stu inner join scores as sc on stu.StuID=sc.StuID) as stsc group by stsc.Name order by newsc desc limit 3;
+-------------+---------+
| Name        | newsc   |
+-------------+---------+
| Shi Qing    | 96.0000 |
| Shi Zhongyu | 85.0000 |
| Xi Ren      | 84.5000 |
+-------------+---------+
3 rows in set (0.00 sec)
------------------------------------------------------------------------------------------------------
#14.2
MariaDB [hellodb]> select stu.StuID, stu.Name, avg(sc.Score) as newsc from students stu inner join scores sc on stu.StuID = sc.StuID group by stu.Name order by newsc desc limit 3 ;
+-------+-------------+---------+
| StuID | Name        | newsc   |
+-------+-------------+---------+
|     6 | Shi Qing    | 96.0000 |
|     1 | Shi Zhongyu | 85.0000 |
|     7 | Xi Ren      | 84.5000 |
+-------+-------------+---------+
3 rows in set (0.00 sec)

15.顯示每門課程課程名稱及學習了這門課的同學的個數

#15.1
MariaDB [hellodb]> select cou.CourseID,cou.Course,countNum.num from courses as cou inner join (select CourseID,count(*) as num from scores group by CourseID) as countNum on cou.CourseID=countNum.CourseID; 
+----------+----------------+-----+
| CourseID | Course         | num |
+----------+----------------+-----+
|        1 | Hamo Gong      |   3 |
|        2 | Kuihua Baodian |   4 |
|        3 | Jinshe Jianfa  |   1 |
|        4 | Taiji Quan     |   1 |
|        5 | Daiyu Zanghua  |   2 |
|        6 | Weituo Zhang   |   2 |
|        7 | Dagou Bangfa   |   2 |
+----------+----------------+-----+
7 rows in set (0.00 sec)
-------------------------------------------------------------------------------------------------------
#15.2
MariaDB [hellodb]> select cou.CourseID, cou.Course, count(sc.StuID) as num  from courses cou left join scores as sc on cou.CourseID = sc.CourseID group by cou.Course;
+----------+----------------+-----+
| CourseID | Course         | num |
+----------+----------------+-----+
|        7 | Dagou Bangfa   |   2 |
|        5 | Daiyu Zanghua  |   2 |
|        1 | Hamo Gong      |   3 |
|        3 | Jinshe Jianfa  |   1 |
|        2 | Kuihua Baodian |   4 |
|        4 | Taiji Quan     |   1 |
|        6 | Weituo Zhang   |   2 |
+----------+----------------+-----+
7 rows in set (0.00 sec)

16.顯示其年齡大於平均年齡的同學的名字

MariaDB [hellodb]> select Name,Age from students where Age > (select avg(Age) from students);
+--------------+-----+
| Name         | Age |
+--------------+-----+
| Xie Yanke    |  53 |
| Ding Dian    |  32 |
| Shi Qing     |  46 |
| Tian Boguang |  33 |
| Sun Dasheng  | 100 |
+--------------+-----+
5 rows in set (0.00 sec)

17.顯示其學習的課程爲第1、2,4或第7門課的同學的名字

MariaDB [hellodb]> select StuID,Name from students where StuID in(select distinct StuID from scores where CourseID in (1,2,4,7));
+-------+-------------+
| StuID | Name        |
+-------+-------------+
|     1 | Shi Zhongyu |
|     2 | Shi Potian  |
|     3 | Xie Yanke   |
|     4 | Ding Dian   |
|     5 | Yu Yutong   |
|     6 | Shi Qing    |
|     7 | Xi Ren      |
|     8 | Lin Daiyu   |
+-------+-------------+
8 rows in set (0.00 sec)

18.顯示其成員數最少的3個的班級的同學中年齡大於同班同學平均年齡的同學

MariaDB [hellodb]>
MariaDB [hellodb]> select ClassID, count(*) as num  from students group by ClassID having ClassID is not null order by num limit 3;
+---------+-----+
| ClassID | num |
+---------+-----+
|       5 |   1 |
|       2 |   3 |
|       7 |   3 |
+---------+-----+
3 rows in set (0.00 sec)

MariaDB [hellodb]>
MariaDB [hellodb]>
MariaDB [hellodb]> select * from students as stu inner join (select ClassID, count(*) as num  from students group by ClassID having ClassID is not null order by num limit 3) as sclass on stu.ClassID = sclass.ClassID;
+-------+--------------+-----+--------+---------+-----------+---------+-----+
| StuID | Name         | Age | Gender | ClassID | TeacherID | ClassID | num |
+-------+--------------+-----+--------+---------+-----------+---------+-----+
|     1 | Shi Zhongyu  |  22 | M      |       2 |         3 |       2 |   3 |
|     3 | Xie Yanke    |  53 | M      |       2 |        16 |       2 |   3 |
|     6 | Shi Qing     |  46 | M      |       5 |      NULL |       5 |   1 |
|     8 | Lin Daiyu    |  17 | F      |       7 |      NULL |       7 |   3 |
|    13 | Tian Boguang |  33 | M      |       2 |      NULL |       2 |   3 |
|    18 | Hua Rong     |  23 | M      |       7 |      NULL |       7 |   3 |
|    20 | Diao Chan    |  19 | F      |       7 |      NULL |       7 |   3 |
+-------+--------------+-----+--------+---------+-----------+---------+-----+
7 rows in set (0.00 sec)

MariaDB [hellodb]>
MariaDB [hellodb]>
MariaDB [hellodb]> select stu.ClassID, avg(stu.Age) from students as stu inner join (select ClassID, count(*) as num  from students group by ClassID having ClassID is not null order by num limit 3) as sclass on stu.ClassID = sclass.ClassID group by stu.ClassID;
+---------+--------------+
| ClassID | avg(stu.Age) |
+---------+--------------+
|       2 |      36.0000 |
|       5 |      46.0000 |
|       7 |      19.6667 |
+---------+--------------+
3 rows in set (0.00 sec)

MariaDB [hellodb]>
MariaDB [hellodb]>
MariaDB [hellodb]> select * from students inner join (select stu.ClassID, avg(stu.Age) as avgAge from students as stu inner join (select ClassID, count(*) as num  from students group by ClassID having ClassID is not null order by num limit 3) as sclass on stu.ClassID = sclass.ClassID group by stu.ClassID) as avgtab on students.ClassID = avgtab.ClassID and students.age > avgtab.avgAge;
+-------+-----------+-----+--------+---------+-----------+---------+---------+
| StuID | Name      | Age | Gender | ClassID | TeacherID | ClassID | avgAge  |
+-------+-----------+-----+--------+---------+-----------+---------+---------+
|     3 | Xie Yanke |  53 | M      |       2 |        16 |       2 | 36.0000 |
|    18 | Hua Rong  |  23 | M      |       7 |      NULL |       7 | 19.6667 |
+-------+-----------+-----+--------+---------+-----------+---------+---------+
2 rows in set (0.00 sec)

MariaDB [hellodb]>

19.顯示其成員數最少爲3個的班級的同學中年齡大於同班同學平均年齡的同學

MariaDB [hellodb]> select ClassID, count(*) as num  from students group by ClassID having ClassID is not null and num >= 3;
+---------+-----+
| ClassID | num |
+---------+-----+
|       1 |   4 |
|       2 |   3 |
|       3 |   4 |
|       4 |   4 |
|       6 |   4 |
|       7 |   3 |
+---------+-----+
6 rows in set (0.00 sec)

MariaDB [hellodb]>
MariaDB [hellodb]>
MariaDB [hellodb]> select * from students as stu inner join ( select ClassID, count(*) as num  from students group by ClassID having ClassID is not null and num >= 3 ) as sclass on stu.ClassID = sclass.ClassID;
+-------+---------------+-----+--------+---------+-----------+---------+-----+
| StuID | Name          | Age | Gender | ClassID | TeacherID | ClassID | num |
+-------+---------------+-----+--------+---------+-----------+---------+-----+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |       2 |   3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |       1 |   4 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |       2 |   3 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |       4 |   4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |       3 |   4 |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |       3 |   4 |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |       7 |   3 |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |       6 |   4 |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |       3 |   4 |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |       6 |   4 |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |       1 |   4 |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |       2 |   3 |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |       3 |   4 |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |       4 |   4 |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |       1 |   4 |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |       4 |   4 |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |       7 |   3 |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |       6 |   4 |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |       7 |   3 |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |       6 |   4 |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |       1 |   4 |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |       4 |   4 |
+-------+---------------+-----+--------+---------+-----------+---------+-----+
22 rows in set (0.00 sec)

MariaDB [hellodb]>
MariaDB [hellodb]>
MariaDB [hellodb]> select * from students inner join (select stu.ClassID, avg(stu.Age) as avgAge from students as stu inner join (select ClassID, count(*) as num  from students group by ClassID having ClassID is not null and num >= 3) as sclass on stu.ClassID = sclass.ClassID group by stu.ClassID) as avgtab on students.ClassID = avgtab.ClassID and students.age > avgtab.avgAge;
+-------+---------------+-----+--------+---------+-----------+---------+---------+
| StuID | Name          | Age | Gender | ClassID | TeacherID | ClassID | avgAge  |
+-------+---------------+-----+--------+---------+-----------+---------+---------+
|     2 | Shi Potian    |  22 | M      |       1 |         7 |       1 | 20.5000 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |       2 | 36.0000 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |       4 | 24.7500 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |       3 | 20.2500 |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |       6 | 20.7500 |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |       1 | 20.5000 |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |       4 | 24.7500 |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |       7 | 19.6667 |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |       6 | 20.7500 |
+-------+---------------+-----+--------+---------+-----------+---------+---------+
9 rows in set (0.00 sec)

MariaDB [hellodb]>

20.統計各班級中年齡大於全校同學平均年齡的同學

MariaDB [hellodb]> select * from students where Age > (select avg(Age) from students where classID is not null) and classID is not null;
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name         | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
|     3 | Xie Yanke    |  53 | M      |       2 |        16 |
|     4 | Ding Dian    |  32 | M      |       4 |         4 |
|     5 | Yu Yutong    |  26 | M      |       3 |         1 |
|     6 | Shi Qing     |  46 | M      |       5 |      NULL |
|    13 | Tian Boguang |  33 | M      |       2 |      NULL |
|    17 | Lin Chong    |  25 | M      |       4 |      NULL |
+-------+--------------+-----+--------+---------+-----------+
6 rows in set (0.00 sec)
看到這條信息,首先謝謝您😘其次表示本次分享到這就完全結束了,歡迎下次光臨!(~ ̄▽ ̄)~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章