当前位置:网站首页>【数据库】MySQL多表查询(一)
【数据库】MySQL多表查询(一)
2022-04-23 04:46:00 【明璐花生牛奶】
练习四(一) 多表查询
一、连接查询
- 查询女学生的学生学号及总成绩
首先看一下我们jxgl数据库里的表格:

查询女学生的学生学号和总成绩,我们发现sc表里没有性别这一栏,student表里有性别,但是student表里没有成绩。
因此通过这一步操作我们便引入里这一章的主要内容---->多表查询;
方便观察,我这里先贴一下sc表和student表


操作如下:
![]()
出现了下面的这种状况,为什么?
MySQL的sql_mode是only_full_group_by的时候,在不使用group by并且select后面出现聚集函数的话,那么所有被select的都应该使用聚集函数(比如说本例中的sum函数)。否则会报出来这样的错误;
Tips: 在select指定的字段要么就要包含在group by语句的后面,作为分组的依据;要么就被包含在聚合函数里

2.查询李勇同学所选的课程号及成绩
课程号和成绩在sc表里,但是学生的姓名在student表里,因此又是多表查询
因此直接了当上操作:

可能觉得还不错,但是认真看看,其实是有问题的:select出是sc表的全部啊!
有问题!!!!!!!!!!!!!


然后我们看一下之前选的:
3.查询李新老师所授课程的课程名称
首先课程名称在course表里:

然后老师的姓名在teacher表里:

但是我们观察这两张表我们很容易发现,这两张表没啥能对的上的字段,因此需要例外一张teaching表:

进行一下操作:

4.查询女教师所授课程的课程号及课程名称

5.查询姓“王”的学生所学的课程名称
这里就需要通配符%了

6.查询选修“数据库”课程且成绩在 80 到 90 之间的学生学号及成绩

7.查询选修“C03”课程的学生的平均年龄

但是如果甲方很刁钻,就要两位小数的,就需要cast函数这样的:

但是甲方又觉得这个字段的名字太长了,换成平均年龄

8.查询学习课程名为“数据库”的学生学号和姓名

9.查询”李新”教师任课的课程号,选修其课程的学生的学号和成绩

10. 查询在第 3 学期所开课程的课程名称,选修其课程的学生学号和成绩

版权声明
本文为[明璐花生牛奶]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_54438368/article/details/124264582
边栏推荐
- Mysql50 basic exercises
- Unity RawImage背景无缝连接移动
- Spark case - wordcount
- MySQL - data read / write separation, multi instance
- Eksctl deploying AWS eks
- Innovation training (IV) preliminary preparation - server
- Last day of 2017
- Flink case - Kafka, MySQL source
- POI export message list (including pictures)
- Detailed explanation of life cycle component of jetpack
猜你喜欢

Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU

Record the ThreadPoolExecutor main thread waiting for sub threads

Innovation training (IV) preliminary preparation - server

2021数学建模国赛一等奖经验总结与分享

Innovation training (IX) integration

Spark optimization

New terminal play method: script guidance independent of technology stack

win10, mysql-8.0.26-winx64. Zip installation

CLion+OpenCV identify ID number - detect ID number

Wechat payment function
随机推荐
Innovation training (VII) FBV view & CBV view
补充番外14:cmake实践项目笔记(未完待续4/22)
Open the past and let's start over.
test
383. Ransom letter
General enumeration constant class
Solutions to the failure of sqoop connection to MySQL
Wechat payment function
IDE idea automatic compilation and configuration of on update action and on frame deactivation
Code007 -- determine whether the string in parentheses matches
Leetcode005 -- delete duplicate elements in the array in place
Unity攝像頭跟隨鼠標旋轉
Innovative practice of short video content understanding and generation technology in meituan
Introduction to raspberry pie 3B - system installation
MySQL - data read / write separation, multi instance
leetcode002--将有符号整数的数字部分反转
Spark optimization
C language: spoof games
Go reflection rule
Innovation training (IX) integration