当前位置:网站首页>【数据库】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
边栏推荐
- Innovation training (II) task division
- unity摄像机旋转带有滑动效果(自转)
- Open the past and let's start over.
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- Excel protects worksheets and workbooks from damage
- General enumeration constant class
- Recommended scheme of national manufactured electronic components for intelligent electronic scales
- The programmer starts the required application with one click of window bat
- Innovation training (IV) preliminary preparation - server
- leetcode008--实现strStr()函数
猜你喜欢

Arduino UNO r3+LCD1602+DHT11

QML advanced (IV) - drawing custom controls

229. Find mode II

Innovative practice of short video content understanding and generation technology in meituan
![[paper reading] [3D object detection] voxel transformer for 3D object detection](/img/a2/9f66789cc12fad99491309717cf418.png)
[paper reading] [3D object detection] voxel transformer for 3D object detection

数据孤岛是什么?为什么2022年仍然存在数据孤岛?

Pixel mobile phone brick rescue tutorial

Druid -- JDBC tool class case
![[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN](/img/c5/3b3f9cf9a39bf14a68ac100294ca6c.png)
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN

QML进阶(四)-绘制自定义控件
随机推荐
Leetcode005 -- delete duplicate elements in the array in place
Innovation training (IX) integration
test
List< Map> Replication: light copy and deep copy
Basic operation of sequence table
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Recursive call -- Enumeration of permutations
C language: spoof games
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
Innovation training (VII) FBV view & CBV view
Solutions to the failure of sqoop connection to MySQL
POI export message list (including pictures)
Record the blind injection script
[paper reading] [3D target detection] point transformer
Key points of AWS eks deployment and differences between console and eksctl creation
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
Leetcode008 -- implement strstr() function
getprop 属性
Eight misunderstandings that should be avoided in data visualization
Jetpack -- lifecycle usage and source code analysis