MySQL 三表查询范例
2022/9/11 2:24:33
本文主要是介绍MySQL 三表查询范例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
范例:
有三张表,分别存放学生信息,课程信息和成绩
求:每个学生的姓名以及每门课的分数
mysql> 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) mysql> 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) mysql> 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) mysql> select st.name,co.course,sc.score from students st inner join scores sc on st.StuID=sc.StuID inner join courses co on sc.courseid=co.courseid; +-------------+----------------+-------+ | 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 | | Shi Qing | Hamo Gong | 96 | | Xi Ren | Hamo Gong | 86 | | Xi Ren | Dagou Bangfa | 83 | | Lin Daiyu | Taiji Quan | 57 | | Lin Daiyu | Jinshe Jianfa | 93 | +-------------+----------------+-------+ 15 rows in set (0.00 sec) 先查出学生姓名和考试的成绩。 mysql> select st.name,sc.courseID,sc.score from students st inner join scores sc on st.StuID=sc.StuID; +-------------+----------+-------+ | name | courseID | score | +-------------+----------+-------+ | Shi Zhongyu | 2 | 77 | | Shi Zhongyu | 6 | 93 | | Shi Potian | 2 | 47 | | Shi Potian | 5 | 97 | | Xie Yanke | 2 | 88 | | Xie Yanke | 6 | 75 | | Ding Dian | 5 | 71 | | Ding Dian | 2 | 89 | | Yu Yutong | 1 | 39 | | Yu Yutong | 7 | 63 | | Shi Qing | 1 | 96 | | Xi Ren | 1 | 86 | | Xi Ren | 7 | 83 | | Lin Daiyu | 4 | 57 | | Lin Daiyu | 3 | 93 | +-------------+----------+-------+ 15 rows in set (0.00 sec) 查出学生姓名和考试成绩以后根据courseID来确定对应的课程。 mysql> select st.name,co.course,sc.score from students st inner join scores sc on st.StuID=sc.StuID inner join courses co on sc.courseid=co.courseid; +-------------+----------------+-------+ | 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 | | Shi Qing | Hamo Gong | 96 | | Xi Ren | Hamo Gong | 86 | | Xi Ren | Dagou Bangfa | 83 | | Lin Daiyu | Taiji Quan | 57 | | Lin Daiyu | Jinshe Jianfa | 93 | +-------------+----------------+-------+ 15 rows in set (0.00 sec)
这篇关于MySQL 三表查询范例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-07MySQL读写分离入门:轻松掌握数据库读写分离技术
- 2024-12-07MySQL读写分离入门教程
- 2024-12-07MySQL分库分表入门详解
- 2024-12-07MySQL分库分表入门指南
- 2024-12-07MySQL慢查询入门:快速掌握性能优化技巧
- 2024-12-07MySQL入门:新手必读的简单教程
- 2024-12-07MySQL入门:从零开始学习MySQL数据库
- 2024-12-07MySQL索引入门:新手快速掌握MySQL索引技巧
- 2024-12-06BinLog学习:MySQL数据库BinLog入门教程
- 2024-12-06Binlog学习:MySQL数据库的日志管理入门教程