当前位置:网站首页>【数据库】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
边栏推荐
- PHP+MySQL 制作留言板
- Excel protects worksheets and workbooks from damage
- QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
- Unity摄像头跟随鼠标旋转
- Recommended scheme for national production of electronic components for wireless charging
- Coinbase: basic knowledge, facts and statistics about cross chain bridge
- Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
- QML advanced (V) - realize all kinds of cool special effects through particle simulation system
- leetcode001--返回和为target的数组元素的下标
- Phishing for NFT
猜你喜欢
PIP3 installation requests Library - the most complete pit sorting
Inverse system of RC low pass filter
AWS eks add cluster user or Iam role
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
[paper reading] [3D target detection] point transformer
Innovation training (VI) routing
229. Find mode II
Key points of AWS eks deployment and differences between console and eksctl creation
Go reflection rule
redis数据类型有哪些
随机推荐
Better way to read configuration files than properties
PIP3 installation requests Library - the most complete pit sorting
Unity3d practical skills - theoretical knowledge base (I)
win10, mysql-8.0.26-winx64. Zip installation
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
[paper reading] [3D object detection] voxel transformer for 3D object detection
Spark small case - RDD, spark SQL
QML进阶(四)-绘制自定义控件
Leetcode002 -- inverts the numeric portion of a signed integer
Leetcode001 -- returns the subscript of the array element whose sum is target
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
SQL statement for adding columns in MySQL table
Solutions to the failure of sqoop connection to MySQL
Create VPC in AWS console (no plate)
Getprop property
Leetcode005 -- delete duplicate elements in the array in place
Leetcode004 -- Roman numeral to integer
Innovation training (V) configuration information
redis数据类型有哪些
做数据可视化应该避免的8个误区