数据库
首页 > 数据库> > Linux - Mysql Select

Linux - Mysql Select

作者:互联网

04:54:52(root@localhost) [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 |
+-------+---------------+-----+--------+---------+-----------+

05:03:57(root@localhost) [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)

 

 

05:36:42(root@localhost) [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)

 

 

05:37:11(root@localhost) [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.在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄

04:57:08(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.00 sec)

2.以ClassID为分组依据,显示每组的平均年龄

04:58:23(root@localhost) [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 |
+---------+----------+
8 rows in set (0.00 sec)

3.显示第2题中平均年龄大于30的分组及平均年龄

04:58:29(root@localhost) [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 |
+---------+----------+
3 rows in set (0.00 sec)

4.显示以L开头的名字的同学的信息

05:06:20(root@localhost) [hellodb]> select * from students where name like 'L%';
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)

5.显示TeacherID非空的同学的相关信息

05:07:40(root@localhost) [hellodb]> select * from students where teacherid is not NULL;
+-------+-------------+-----+--------+---------+-----------+
| 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.以年龄排序后,显示年龄最大的前10位同学的信息

05:14:49(root@localhost) [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 |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
+-------+--------------+-----+--------+---------+-----------+
10 rows in set (0.00 sec)

7.查询年龄大于等于20岁,小于等于25岁的同学的信息

05:14:55(root@localhost) [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 |
| 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 |
| 23 | Ma Chao | 23 | M | 4 | NULL |
+-------+---------------+-----+--------+---------+-----------+
8 rows in set (0.00 sec)

8.以ClassID分组,显示每班的同学的人数

05:16:07(root@localhost) [hellodb]> select classid,count(*) from students group by classid;
+---------+----------+
| classid | count(*) |
+---------+----------+
| NULL | 2 |
| 1 | 4 |
| 2 | 3 |
| 3 | 4 |
| 4 | 4 |
| 5 | 1 |
| 6 | 4 |
| 7 | 3 |
+---------+----------+
8 rows in set (0.00 sec)

9.以Gender分组,显示其年龄之和

05:17:11(root@localhost) [hellodb]> select age,count(*),sum(age) from students group by age;
+-----+----------+----------+
| age | count(*) | sum(age) |
+-----+----------+----------+
| 17 | 2 | 34 |
| 18 | 1 | 18 |
| 19 | 5 | 95 |
| 20 | 2 | 40 |
| 21 | 1 | 21 |
| 22 | 3 | 66 |
| 23 | 3 | 69 |
| 25 | 1 | 25 |
| 26 | 1 | 26 |
| 27 | 1 | 27 |
| 32 | 1 | 32 |
| 33 | 1 | 33 |
| 46 | 1 | 46 |
| 53 | 1 | 53 |
| 100 | 1 | 100 |
+-----+----------+----------+
15 rows in set (0.00 sec)

10.以ClassID分组,显示其平均年龄大于25的班级

05:19:48(root@localhost) [hellodb]> select classid,avg(age) from students group by classid having avg(age) > 25;
+---------+----------+
| classid | avg(age) |
+---------+----------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+----------+
3 rows in set (0.00 sec)

11.以Gender分组,显示各组中年龄大于25的学员的年龄之和

05:35:14(root@localhost) [hellodb]> select gender,sum(age) from (select gender,age from students where age>25) as t group by gender;
+--------+----------+
| gender | sum(age) |
+--------+----------+
| M | 317 |
+--------+----------+
1 row in set (0.00 sec)

12.显示前5位同学的姓名、课程及成绩

05:46:02(root@localhost) [hellodb]> select st.name,co.course,sc.score from (select * from students limit 5 ) as st inner join scores sc on sc.stuid=st.stuuid inne
+-------------+----------------+-------+
| name | course | score |
+-------------+----------------+-------+
| Shi Zhongyu | Kuihua Baodian | 77 |
| Shi Zhongyu | Weituo Zhang | 93 |
| Shi Potian | Kuihua Baodian | 47 |
| Shi Potian | Daiyu Zanghua | 97 |
| Xie Yanke | Kuihua Baodian | 88 |
| Xie Yanke | Weituo Zhang | 75 |
| Ding Dian | Daiyu Zanghua | 71 |
| Ding Dian | Kuihua Baodian | 89 |
| Yu Yutong | Hamo Gong | 39 |
| Yu Yutong | Dagou Bangfa | 63 |
+-------------+----------------+-------+
10 rows in set (0.00 sec)

13.显示其成绩高于80的同学的名称及课程

05:48:13(root@localhost) [hellodb]> select st.name,co.course,sc.score from students st inner join scores sc on sc.stuid=st.stuid inner join courses co on sc.courseid=co.courseid and sc.score > 80;
+-------------+----------------+-------+
| name | course | score |
+-------------+----------------+-------+
| Shi Zhongyu | Weituo Zhang | 93 |
| Shi Potian | Daiyu Zanghua | 97 |
| Xie Yanke | Kuihua Baodian | 88 |
| Ding Dian | Kuihua Baodian | 89 |
| Shi Qing | Hamo Gong | 96 |
| Xi Ren | Hamo Gong | 86 |
| Xi Ren | Dagou Bangfa | 83 |
| Lin Daiyu | Jinshe Jianfa | 93 |
+-------------+----------------+-------+
8 rows in set (0.00 sec)

14.取每位同学各门课的平均成绩,显示成绩前三名的同学的姓名和平均成绩

05:52:33(root@localhost) [hellodb]> select st.name,co.avg from students st inner join (select stuid,avg(score) avg from scores group by stuid) as co on stt.stuid=co.stuid;
+-------------+---------+
| name | avg |
+-------------+---------+
| Shi Zhongyu | 85.0000 |
| Shi Potian | 72.0000 |
| Xie Yanke | 81.5000 |
| Ding Dian | 80.0000 |
| Yu Yutong | 51.0000 |
| Shi Qing | 96.0000 |
| Xi Ren | 84.5000 |
| Lin Daiyu | 75.0000 |
+-------------+---------+
8 rows in set (0.00 sec)

15. 显示每门课程课程名称及学习了这门课的同学的个数

06:00:13(root@localhost) [hellodb]> select co.course,sc.numofstu from courses co inner join (select courseid,count(stuid) numofstu from scores group by courseid) as sc where co.courseid=sc.courseid;
+----------------+----------+
| course | numofstu |
+----------------+----------+
| Hamo Gong | 3 |
| Kuihua Baodian | 4 |
| Jinshe Jianfa | 1 |
| Taiji Quan | 1 |
| Daiyu Zanghua | 2 |
| Weituo Zhang | 2 |
| Dagou Bangfa | 2 |
+----------------+----------+
7 rows in set (0.00 sec)

16. 显示其年龄大于平均年龄的同学的名字

06:16:41(root@localhost) [hellodb]> select st1.stuid,st1.name,st1.age,st2.avgage from students st1 inner join (select avg(age) avgage from students) as st2 on st1.age>st2.avgage;
+-------+--------------+-----+---------+
| stuid | name | age | avgage |
+-------+--------------+-----+---------+
| 3 | Xie Yanke | 53 | 27.4000 |
| 4 | Ding Dian | 32 | 27.4000 |
| 6 | Shi Qing | 46 | 27.4000 |
| 13 | Tian Boguang | 33 | 27.4000 |
| 25 | Sun Dasheng | 100 | 27.4000 |
+-------+--------------+-----+---------+
5 rows in set (0.00 sec)

17. 显示其学习的课程为第1、2,4或第7门课的同学的名字

06:19:39(root@localhost) [hellodb]> select name,classid from students where classid=1 or classid=2 or classid=4 or classid=7;
+--------------+---------+
| name | classid |
+--------------+---------+
| Shi Zhongyu | 2 |
| Shi Potian | 1 |
| Xie Yanke | 2 |
| Ding Dian | 4 |
| Lin Daiyu | 7 |
| Wen Qingqing | 1 |
| Tian Boguang | 2 |
| Duan Yu | 4 |
| Xu Zhu | 1 |
| Lin Chong | 4 |
| Hua Rong | 7 |
| Diao Chan | 7 |
| Xiao Qiao | 1 |
| Ma Chao | 4 |
+--------------+---------+
14 rows in set (0.00 sec)

18. 显示其成员数最少为3个的班级的同学中年龄大于同班同学平均年龄的同学

06:26:37(root@localhost) [hellodb]> select classid,count(*) from students group by classid having count(*) > 3;
+---------+----------+
| classid | count(*) |
+---------+----------+
| 1 | 4 |
| 3 | 4 |
| 4 | 4 |
| 6 | 4 |
+---------+----------+
4 rows in set (0.00 sec)

06:26:43(root@localhost) [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 |
+---------+----------+
8 rows in set (0.00 sec)

06:26:51(root@localhost) [hellodb]> select st1.name,st1.age,st1.classid from students st1 inner join (select classid,count(*) from students group by classid having count(*) > 3) as st2 on st2.classid=st1.classid inner join (select classid,avg(age) avgage from students group by classid) as st3 on st2.classiid=st3.classid and st1.age > st3.avgage;
+---------------+-----+---------+
| name | age | classid |
+---------------+-----+---------+
| Shi Potian | 22 | 1 |
| Ding Dian | 32 | 4 |
| Yu Yutong | 26 | 3 |
| Yuan Chengzhi | 23 | 6 |
| Xu Zhu | 21 | 1 |
| Lin Chong | 25 | 4 |
| Huang Yueying | 22 | 6 |
+---------------+-----+---------+
7 rows in set (0.00 sec)

 

 

19. 统计各班级中年龄大于全校同学平均年龄的同学

06:33:13(root@localhost) [hellodb]> select * from students st1 inner join (select avg(age) age from students) as st2 on st1.age>st2.age ;
+-------+--------------+-----+--------+---------+-----------+---------+
| StuID | Name | Age | Gender | ClassID | TeacherID | age |
+-------+--------------+-----+--------+---------+-----------+---------+
| 3 | Xie Yanke | 53 | M | 2 | 16 | 27.4000 |
| 4 | Ding Dian | 32 | M | 4 | 4 | 27.4000 |
| 6 | Shi Qing | 46 | M | 5 | NULL | 27.4000 |
| 13 | Tian Boguang | 33 | M | 2 | NULL | 27.4000 |
| 25 | Sun Dasheng | 100 | M | NULL | NULL | 27.4000 |
+-------+--------------+-----+--------+---------+-----------+---------+
5 rows in set (0.00 sec)

 

 

标签:classid,students,age,Mysql,Select,Linux,NULL,select,localhost
来源: https://www.cnblogs.com/noise/p/14878765.html