首页 > 其他分享 >联合查询(多表查询)

联合查询(多表查询)

时间:2024-07-04 17:28:19浏览次数:3  
标签:多表 student -- 查询 course score 联合 id select

  • 多表查询是对多张表的数据取笛卡尔积(关联查询可以对关联表使用别名)

在这里插入图片描述

  • 数据准备
insert into classes(name, `desc`) values
('计算机系2019级1班', '学习了计算机原理、C和Java语言、数据结构和算法'),
('中文系2019级3班','学习了中国传统文学'),
('自动化2019级5班','学习了机械自动化');
insert into student(sn, name, qq_mail, classes_id) values
('09982','黑旋风李逵','[email protected]',1),
('00835','菩提老祖',null,1),
('00391','白素贞',null,1),
('00031','许仙','[email protected]',1),
('00054','不想毕业',null,1),
('51234','好好说话','[email protected]',2),
('83223','tellme',null,2),
('09527','老外学中文','[email protected]',2);
insert into course(name) values
('Java'),('中国传统文化'),('计算机原理'),('语文'),('高阶数学'),('英文')
insert into score(score, student_id, course_id) values
-- 黑旋风李逵
(70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),
-- 菩提老祖
(60, 2, 1),(59.5, 2, 5),
-- 白素贞
(33, 3, 1),(68, 3, 3),(99, 3, 5),
-- 许仙
(67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),
-- 不想毕业
(81, 5, 1),(37, 5, 5),
-- 好好说话
(56, 6, 2),(43, 6, 4),(79, 6, 6),
-- tellme
(80, 7, 2),(92, 7, 6);


classes

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

student

在这里插入图片描述

course

在这里插入图片描述

score

在这里插入图片描述

  • 内连接(表示两个表都存在的结果)
select 字段 from 表1 别名1 [inner] join 表2 别名2 on 连接条件 and 其他条件;
select 字段 from 表1 别名1,表2 别名2 where 连接条件 and 其他条件;

案列:查询每个同学姓名 课程名字 分数

//方法一
select student.name,course.name,score from student,course,score where student.id = score.student_id and score.course_id = course.id;

在这里插入图片描述

--方法二
 select student.name,course.name,score from student join score on student.id = score.student_id join course on score.course_id = course.id;
 

在这里插入图片描述

  • 外连接

外连接分为左外连接和右外连接。如果联合查询,左侧的表完全显示我们就说是左外连接(如果这个记录在右表中没有匹配,就把对应的列填成NULL) ;右侧的表完全显示我们就说是右外连接。

-- 左外连接,表1完全显示
select 字段名  from 表名1 left join 表名2 on 连接条件;
-- 右外连接,表2完全显示
select 字段 from 表名1 right join 表名2 on 连接条件;
--案列
select * from score sco right join student stu on stu.id=sco.student_id;

在这里插入图片描述

  • 自连接

自连接是指在同一张表连接自身进行查询

select ... from 表1,表1 where 条件
select ... from 表1 join 表1 on 条件

-案列
显示所有“计算机原理”成绩比“Java”成绩高的成绩信息
-起别名,因为两个表的名字是一样的
select * from  score as s1,score as s2;
-筛选出java和计算机组成原理的分数
select * from  score as s1,score as s2 where s1.student_id = s2.student_id and s1.course_id = 3 and s2.course_id =1;
-进行结果查询
 select * from  score as s1,score as s2 where s1.student_id = s2.student_id and s1.course_id = 3 and s2.course_id =1 and s1.score > s2.score;

在这里插入图片描述

  • 子查询(简单sql拼成复杂sql)

  • 子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询

  • 单行子查询:返回一行记录的子查询

查询与“不想毕业” 同学的同班同学:
select * from student where classes_id=(select classes_id from student where
name='不想毕业');

在这里插入图片描述

  • 多行子查询:返回多行记录的子查询
查询“语文”或“英文”课程的成绩信息
-- 使用IN
select * from score where course_id in (select id from course where
name='语文' or name='英文');
-- 使用 NOT IN
select * from score where course_id not in (select id from course where
name!='语文' and name!='英文');

可以使用多列包含:
-- 插入重复的分数:score, student_id, course_id列重复
insert into score(score, student_id, course_id) values
-- 黑旋风李逵
(70.5, 1, 1),(98.5, 1, 3),
-- 菩提老祖
(60, 2, 1);
-- 查询重复的分数
SELECT
 * 
FROM
 score 
WHERE
 ( score, student_id, course_id ) IN ( SELECT score, student_id, 
course_id FROM score GROUP BY score, student_id, course_id HAVING
count( 0 ) > 1 );

[NOT] EXISTS关键字:

-- 使用 EXISTS
select * from score sco where exists (select sco.id from course cou 
where (name='语文' or name='英文') and cou.id = sco.course_id);
-- 使用 NOT EXISTS
select * from score sco where not exists (select sco.id from course cou 
where (name!='语文' and name!='英文') and cou.id = sco.course_id);
  • 在from子句中使用子查询:子查询语句出现在from子句中。这里要用到数据查询的技巧,把一个子查询当做一个临时表使用。
查询所有比“中文系2019级3班”平均分高的成绩信息:
-- 获取“中文系2019级3班”的平均分,将其看作临时表
SELECT
 avg( sco.score ) score 
FROM
 score sco
 JOIN student stu ON sco.student_id = stu.id
 JOIN classes cls ON stu.classes_id = cls.id
WHERE
 cls.NAME = '中文系2019级3班';
 
 查询成绩表中,比以上临时表平均分高的成绩:
 SELECT
 * 
FROM
 score sco,
 (
 SELECT
 avg( sco.score ) score 
 FROM
 score sco
 JOIN student stu ON sco.student_id = stu.id
 JOIN classes cls ON stu.classes_id = cls.id
 WHERE
  cls.NAME = '中文系2019级3班'
 ) tmp 
WHERE
 sco.score > tmp.score;
  • 合并查询

  • 在实际应用中,为了合并多个select的执行结果,可以使用集合操作符 union,union all。使用UNION和UNION ALL时,前后查询的结果集中,字段需要一致。

  • union

    该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。

    案例:查询id小于3,或者名字为“英文”的课程:
    select * from course where id<3
    union
    select * from course where name='英文';
    -- 或者使用or来实现
    select * from course where id<3 or name='英文';
    
  • union all

  • 该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行

查询id小于3,或者名字为“Java”的课程
-- 可以看到结果集中出现重复数据Java
select * from course where id<3
union all
select * from course where name='英文';

在这里插入图片描述

标签:多表,student,--,查询,course,score,联合,id,select
From: https://blog.csdn.net/qq_66333706/article/details/140180028

相关文章

  • MySQL单表千万级数据查询优化大家怎么说(评论有亮点)
    题图来自APOD上次写了一篇MySQL优化实战的文章“MySQL千万级数据从190秒优化到1秒全过程”。这篇文章主要还是在实战MySQL优化,所以从造数据到查询SQL优化SQL都没有业务或者其它依赖,优化的技巧也不涉及软件架构就是纯SQL优化。由于笔者经验有限和篇幅限制没有展开讲很多细节,其......
  • 硬核解读,WeTune是如何提升数据库查询重写性能?
    近日,上海交通大学软件学院副院长王肇国和高斯实验室GaussDB数据库优化器专家Ethan联手开展了一场以《智能优化揭秘——GaussDB数据库查询重写的自动挖掘与生成》为主题的技术对谈,深入探讨了WeTune2.0的重写规则与GaussDB的合作落地。直播过程包含对WeTune2.0的项目背景、WeTune......
  • python更新包、pip延时报错,pip check 查询冲突
    pip下载package或者更新package会出现延迟报错的问题,因为python默认使用的是国外镜像,有时候下载非常慢,我们可以选择进行换源,引用国内的镜像资源进行更新或者下载。阿里云: http://mirrors.aliyun.com/pypi/simple/中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/......
  • 不实用iframe,CSS媒体查询依旧生效
    这段SCSS代码定义了用于处理响应式设计和媒体查询的功能和混合器。以下是对这段代码的逐行解释:```scss$mediaMinWidth:1024px;```定义了一个变量`$mediaMinWidth`,其值为1024像素。```scss@functiontranslate-media-condition($c){$condMap:("screen":"......
  • MySQL 中 SQL 查询语句的执行顺序
    在MySQL中,SQL查询的执行顺序通常按照以下顺序进行:FROM:从指定的表中选择数据。WHERE:对数据进行筛选,只选择满足条件的行。GROUPBY:按照指定的列对数据进行分组。SELECT:选择要返回的列或表达式。HAVING:对分组后的数据进行筛选,只选择满足条件的分组。UNION[ALL]ORDERBY:对......
  • 查询分析清理oracle阻塞会话
    1.查询CMS用户当前系统活动的会话selectt.SID,t.SERIAL#,t.STATUS,t.LOGON_TIME,t.LAST_CALL_ET,t.PROGRAM,t.LOCKWAIT,t.BLOCKING_SESSION,t.BLOCKING_SESSION_STATUS,t.BLOCKING_INSTANCE,t.USERNAME,t.PROCESS,t.OSUSER,t.SERVER,t.OSUSER,t.MACHINE,t.TERMINAL,t.PROCESS......
  • MyBatis2(MyBatis基础配置 动态代理 映射器 select 元素 insert 元素 update 元素和del
    目录一、MyBatis基础配置1.MyBatis配置文件2.<configuration>元素3.<enviroments>元素4.<properties>元素5.<typeAliases>元素6.<mappers>元素二、动态代理三、映射器1.映射器与接口2. 映射器的引入 3.映射器的组成 四、select元素参数传递多......
  • MP实现分页条件查询
     publicclassDishController{@AutowiredprivateDishServicedishService;​@GetMapping("/page")publicResult<PageResult>page(DishPageQueryDTOdto){PageResultpage=dishService.page(dto);returnResu......
  • Spring Boot 中 PGSQL 判断打卡点是否经过轨迹优化代码,循环查询物理表修改生成临时表,
    记录一下一个业务问题,流程是这样的,我现在有一个定时任务,5分钟执行一次,更新车辆打卡的情况。现在有20俩车,每辆车都分配了路线,每条路线都有打卡点,每个打卡点分配了不同的时间段,也就是说,一条路线可能有几百个打卡点,这几百个打卡点中每一个都分配了时间段,有可能是1个时间段,比如8......
  • 查询科目余额-带FSG行集
    SELECTGCC.SEGMENT3,(selectffv.DESCRIPTIONfromFND_FLEX_VALUES_VLffvwhereffv.FLEX_VALUE_SET_ID='&科目值集ID'andffv.FLEX_VALUE=gcc.segment3)acc_des,GB.BEGIN_BALANCE_DR-GB.BEGIN_BALANCE_C......