连接查询、自关联、子查询

2024-09-05 02:28
文章标签 查询 连接 关联

本文主要是介绍连接查询、自关联、子查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

连接查询

连接查询概述

  • 内连接:连接两个表时,取的是两个表中都存在的数据(取交集)
  • 左连接:连接两个表时,取的是左表中特有的数据,对于右表中不存在的数据,用null来填充。
  • 右连接:连接两个表时,取的时右表中特有的数据,对于左表中不存在的数据,用null来填充。

image-20240903090147845

image-20240903090308138

image-20240903090351846

image-20240903090447615

  • 数据准备
drop table if exists courses;
create table courses (
courseNo int(10) unsigned primary key auto_increment,
name varchar(10)
);
insert into courses values ('1', '数据库'), ('2', 'qtp'), ('3', 'linux'),
('4', '系统测试'), ('5', '单元测试'), ('6', '测试过程');
drop table if exists scores;
create table scores (
id int(10) unsigned primary key auto_increment,
courseNo int(10),
studentno varchar(10),
score tinyint(4)
);
insert into scores values ('1', '1', '001', '90'), ('2', '1', '002', '75'),
('3', '2', '002', '98'),('4', '3', '001', '86'),('5', '3', '003', '80'),
('6', '4', '004', '79'),('7', '5', '005', '96'),('8', '6', '006', '80');

image-20240903090855359

内连接

  • 语法格式

    select * from 表名1 inner join 表名2 on1.=2.;
    
    • 查询的时两个表的交集的数据
    • 表1的列与表2的列一定是存在关联关系
    • 内连接连接时可以连接多个表
  • 案例

    -- 例1:查询学生信息及学生的成绩
    select * from students inner join scores on students.studentNo=scores.studentNo;
    -- students 起别名为stu,scores起别名为sc
    select * from students as stu inner join scores as  sc on stu.studentNo=sc.studentNo;
    -- 起别名可以不写as
    select * from students  stu inner join scores   sc on stu.studentNo=sc.studentNo;
    select * from students stu , scores sc where   stu.studentNo=sc.studentNo;-- 例2:查询课程信息及课程的成绩
    select * from courses cs  inner join scores  sc on cs.courseNo=sc.courseNo-- 例3、查询王昭君的成绩,要求显示姓名,课程号,成绩
    select stu.name,sc.courseNo,sc.score from students stu inner join scores sc on stu.studentNo=sc.studentNo where stu.name='王昭君'
    
    -- 1、查询学生信息及学生的课程对应的成绩
    select * from students inner join scores on students.studentNo=scores.studentno
    inner join courses on scores.courseNo=courses.courseNo
    -- 2、查询所有学生的数据库成绩,要求显示姓名,课程名,成绩
    select students.name,courses.name,scores.score from students 
    inner join scores on students.studentNo=scores.studentno
    inner join courses on scores.courseNo=courses.courseNo
    -- 3、查询王昭君的数据库成绩,要求显示姓名,课程名,成绩
    select students.name,courses.name,scores.score from students 
    inner join scores on students.studentNo=scores.studentno
    inner join courses on scores.courseNo=courses.courseNo where students.name='王昭君'-- 4、 查询男生中最高成绩,要求显示姓名、课程名、成绩
    select students.name,courses.name,scores.score from students 
    inner join scores on students.studentNo=scores.studentno
    inner join courses on scores.courseNo=courses.courseNo where students.sex='男' order by scores.score desc LIMIT 0,1 -- 等价于limit 1
    

左连接

  • 语法格式

    select * from1 left join2 on1.=2.
    • 左连接查询的是左表特有的数据,对于右表中不存在的数据用null来填充
  • 案例

    -- 例1、查询所有学生的成绩,包括没有成绩的学生
    select * from  students stu left join scores sc on stu.studentNo=sc.studentno
    -- 例2、查询所有学生的成绩,包括没有成绩的学生,需要显示课程名
    select * from  students stu left join scores sc on stu.studentNo=sc.studentno
    left join courses cs on sc.courseNo=cs.courseNo
    

    image-20240903221732290

右连接

  • 语法格式

    select * from1 right join2 on1.=2.
    • 右连接查询的是右表特有的数据,对于左表中不存在的数据用null来填充
  • 案例

    -- 例1、查询所有学生的成绩,包括没有成绩的学生
    select * from scores sc  right join students stu on sc.studentno=stu.studentNo
    -- 例2、查询所有学生的成绩,包括没有成绩的学生,需要显示课程名
    select * from   scores sc right join courses cs on sc.courseNo=cs.courseNo
    right join students stu on stu.studentNo=sc.studentno
    

    image-20240903222349435

自关联

自关联介绍

  • 自连接的应用场景
    • 省、市、区的信息,一般不会分开放在不同的表里面进行存储,而是放在同一个表当中。

image-20240903222739970

  • 数据准备
drop table if exists areas;
create table areas(aid int primary key, atitle varchar(20),pid int);
insert into areas values ('130000', '河北省', NULL), ('130100', '石家庄市','130000'),
('130400','邯郸市','130000'), ('130600', '保定市', '130000'),('130700','张家口市', '130000'),
('130800', '承德市', '130000'),('410000', '河南省', NULL), ('410100', '郑州市', '410000'),
('410300', '洛阳市', '410000'),('410500', '安阳市', '410000'),('410700', '新乡市', '410000'),
('410800', '焦作市', '410000'),('410101', '中原区', '410100'),('410102', '二七区', '410100'),
('410301', '洛龙区', '410300');

自关联实现

  • 要通过自关联进行查询时,当前自关联的表当中一定会存在两个相关联的字段。

  • 自关联要用别名

  • 语法格式

    select * from 表名 as 别名1 inner join 表名 as 别名2 on 别名1.=别名2.
  • 案例

    -- 查询出河南省的所有的市
    select * from areas as a1 inner join areas as a2 on a1.aid=a2.pid where a1.atitle='河南省';
    -- 查询出郑州市所有的区
    select * from areas as a1 inner join areas as a2 on a1.aid=a2.pid where a1.atitle='郑州市';
    -- 查询出河南省所有的市区信息
    select * from areas as a1 inner join areas as a2 on a1.aid=a2.pid 
    left join areas as a3 on a2.pid=a3.pid where a1.atitle='河南省'-- 1、查询河北省所有市的信息
    select * from areas as a1 inner join areas as a2 on a1.aid=a2.pid where a1.atitle='河北省'
    -- 2、查询洛阳市所有区的信息
    select * from areas as a1 inner join areas as a2 on a1.aid=a2.pid where a1.atitle='洛阳市'
    

    image-20240904084046676

image-20240904084108578

子查询

  • 将一条SQL查询的语句嵌入在其他的SQL语句中,被嵌入的SQL语句称之为子查询,其他的SQL称之为主查询
    • 子查询select语句,要么是充当条件,要么充当数据源
    • 子查询语句是一条完整的select语句,且可以单独执行

子查询充当条件

-- 例1、查询王昭君的成绩,要求显示成绩(标量子查询)
select studentNo from students where name='王昭君';
select * from scores where studentNO=(select studentNo from students where name='王昭君');-- 例2、查询18岁的学生的成绩,要求显示成绩(列子查询)
select studentNO from students where age=18;
select score from scores where studentNo in (select studentNO from students where age=18);-- 例3、查询和王昭君同班、同龄的学生信息(行子查询)
select class,age from students where name='王昭君';
select * from students where (class,age)=(select class,age from students where name='王昭君')

子查询充当数据源

-- 例1、查询数据库和系统测试的课程成绩
select * from scores as sc inner join (select * from courses where name in ('数据库','系统测试'))
as co on sc.courseNo=co.courseNo
  • 子查询-特定关键字
    在这里插入图片描述
-- 例1、查询18岁的学生的成绩,要求显示成绩(列子查询)
select studentNo from students where age=18;
select score from scores where studentNo in (select studentNo from students where age=18);
--  =any 和in等价
select score from scores where studentNo =any(select studentNo from students where age=18);
-- some:是any的别称,很少用
select score from scores where studentNo =some(select studentNo from students where age=18);
-- !=all 和not in 等价
select score from scores where studentNo not in (select studentNo from students where age=18);
select score from scores where studentNo !=all (select studentNo from students where age=18);
  • 练习
-- 1、查询大于平均年龄的学生
-- 先查平均年龄
select avg(age) from students ;
select *  from students  where age>(select avg(age) from students);
-- 2、查询年龄在18-20之间的学生的成绩
-- 先查18-20之间的学生的学号
select studentNo from students where age between 18 and  20;
-- 子查询充当条件
select * from scores where studentNo in (select studentNo from students where age between 18 and  20);
-- 子查询充当数据源
select * from scores as sc inner join(select *  from students where age between 18 and  20) as stu
on sc.studentNo=stu.studentNo;

查询演练

  • 准备数据

    /*
    **创建部门表
    */
    drop table if exists departments;
    create table departments (
    deptid int(10) primary key,
    deptname varchar(20) not null -- 部门名称
    );
    insert into departments values ('1001', '市场部');
    insert into departments values ('1002', '测试部');
    insert into departments values ('1003', '开发部');/*
    **创建员工表
    */
    drop table if exists employees;
    create table employees (
    empid int(10) primary key,
    empname varchar(20) not null, -- 姓名
    sex varchar(4) default null, -- 性别
    deptid int(20) default null, -- 部门编号
    jobs varchar(20) default null, -- 岗位
    politicalstatus varchar(20) default null, -- 政治面貌
    leader int(10) default null
    );
    insert into employees values ('1', '王昭君', '女', '1003', '开发', '群众', '9');
    insert into employees values ('2', '诸葛亮', '男', '1003', '开发经理', '群众', null);
    insert into employees values ('3', '张飞', '男', '1002', '测试', '团员', '4');
    insert into employees values ('4', '白起', '男', '1002', '测试经理', '党员', null);
    insert into employees values ('5', '大乔', '女', '1002', '测试', '党员', '4');
    insert into employees values ('6', '孙尚香', '女', '1001', '市场', '党员', '12');
    insert into employees values ('7', '百里玄策', '男', '1001', '市场', '团员', '12');
    insert into employees values ('8', '小乔', '女', '1002', '测试', '群众', '4');
    insert into employees values ('9', '百里守约', '男', '1003', '开发', '党员', '9');
    insert into employees values ('10', '妲己', '女', '1003', '开发', '团员', '9');
    insert into employees values ('11', '李白', '男', '1002', '测试', '团员', '4');
    insert into employees values ('12', '孙膑', '男', '1001', '市场经理', '党员', null);/*
    **创建工资表
    */
    drop table if exists salary;
    create table salary (
    sid int(10) primary key,
    empid int(10) not null,
    salary int(10) not null -- 工资
    );
    insert into salary values ('1', '7', '2100');
    insert into salary values ('2', '6', '2000');
    insert into salary values ('3', '12', '5000');
    insert into salary values ('4', '9', '1999');
    insert into salary values ('5', '10', '1900');
    insert into salary values ('6', '1', '3000');
    insert into salary values ('7', '2', '5500');
    insert into salary values ('8', '5', '2000');
    insert into salary values ('9', '3', '1500');
    insert into salary values ('10', '8', '4000');
    insert into salary values ('11', '11', '2600');
    insert into salary values ('12', '4', '5300');

在这里插入图片描述

  • 练习

    -- 1、列出总人数大于4的部门号和总人数(要统计所有部门的人数,需要使用分组,同时也要使用聚合函数)
    select deptid,count(*) from employees GROUP BY deptid having count(*)>4-- 2、列出开发部和测试部的职工号、姓名
    select deptid from departments where deptname in ('开发部','测试部')
    select empid,empname from employees where deptid in (
    select deptid from departments where deptname in ('开发部','测试部'))-- 3、求出各部门党员的人数,要求显示部门名称
    select * from employees  as emp inner join departments as dep on emp.deptid=dep.deptid
    where emp.politicalstatus='党员';select dep.deptname from employees as emp 
    inner join departments as dep on emp.deptid=dep.deptid
    where emp.politicalstatus='党员'select dep.deptname from employees as emp 
    inner join departments as dep on emp.deptid=dep.deptid
    where emp.politicalstatus='党员' GROUP BY emp.deptid;-- 4、列出市场部的所有女职工的姓名和政治面貌
    select * from employees as emp 
    inner join departments as dep on emp.deptid=dep.deptid
    where emp.sex='女' and dep.deptname='市场部'select emp.empname, emp.politicalstatus from employees as emp
    inner join departments as dep on emp.deptid=dep.deptid
    where emp.sex='女' and dep.deptname='市场部';
    select emp.empname, dep.deptname, sa.salary from employees as emp
    inner join departments as dep on emp.deptid=dep.deptid
    inner join salary as sa on sa.empid=emp.empid;-- 6、显示各部门名和该部门的职工平均工资。
    select dep.deptname,avg(sa.salary) from employees as emp
    inner join departments as dep on emp.deptid=dep.deptid
    inner join salary as sa on sa.empid=emp.empid
    group by emp.deptid;-- 7、显示工资最高的前3名职工的职工号和姓名。
    select emp.empname, dep.deptname, sa.salary from employees as emp
    inner join departments as dep on emp.deptid=dep.deptid
    inner join salary as sa on sa.empid=emp.empid
    order by sa.salary desc limit 3 -- limit 3 等价于 limit 0, 3-- 8、列出工资在1000-2000之间的所有职工姓名。
    select emp.empname, dep.deptname, sa.salary from employees as emp
    inner join departments as dep on emp.deptid=dep.deptid
    inner join salary as sa on sa.empid=emp.empid
    where sa.salary between 1000 and 2000;-- 9、列出工资比王昭君高的员工。(首先查询王昭君的工资)
    -- 查询王昭君的工资
    select sa.salary from employees as emp
    inner join salary as sa on sa.empid=emp.empid
    where emp.empname='王昭君'
    select emp.empname, sa.salary from employees as emp
    inner join salary as sa on sa.empid=emp.empid
    where sa.salary > (
    select sa.salary from employees as empinner join salary as sa on sa.empid=emp.empid
    where emp.empname='王昭君'
    );-- 10、列出每个部门中工资小于本部门平均工资的员工信息。(首先查询出每个部门的平均工资)
    -- 查询出每个部门的平均工资
    select emp.deptid, avg(sa.salary) as avg_salary from employees as emp
    inner join departments as dep on emp.deptid=dep.deptid
    inner join salary as sa on sa.empid=emp.empid
    group by emp.deptid
    select emp.deptid, emp.empname, sa.salary from employees as emp
    inner join salary as sa on sa.empid=emp.empid
    inner join (
    select emp.deptid, avg(sa.salary) as avg_salary from employees as emp
    inner join departments as dep on emp.deptid=dep.deptid
    inner join salary as sa on sa.empid=emp.empid
    group by emp.deptid
    ) as c on emp.deptid=c.deptid
    where sa.salary < c.avg_salary;

这篇关于连接查询、自关联、子查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137712

相关文章

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

Python3.6连接MySQL的详细步骤

《Python3.6连接MySQL的详细步骤》在现代Web开发和数据处理中,Python与数据库的交互是必不可少的一部分,MySQL作为最流行的开源关系型数据库管理系统之一,与Python的结合可以实... 目录环境准备安装python 3.6安装mysql安装pymysql库连接到MySQL建立连接执行S

浅谈mysql的sql_mode可能会限制你的查询

《浅谈mysql的sql_mode可能会限制你的查询》本文主要介绍了浅谈mysql的sql_mode可能会限制你的查询,这个问题主要说明的是,我们写的sql查询语句违背了聚合函数groupby的规则... 目录场景:问题描述原因分析:解决方案:第一种:修改后,只有当前生效,若是mysql服务重启,就会失效;

MySQL多列IN查询的实现

《MySQL多列IN查询的实现》多列IN查询是一种强大的筛选工具,它允许通过多字段组合快速过滤数据,本文主要介绍了MySQL多列IN查询的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析与优化1.

Spring Boot 整合 MyBatis 连接数据库及常见问题

《SpringBoot整合MyBatis连接数据库及常见问题》MyBatis是一个优秀的持久层框架,支持定制化SQL、存储过程以及高级映射,下面详细介绍如何在SpringBoot项目中整合My... 目录一、基本配置1. 添加依赖2. 配置数据库连接二、项目结构三、核心组件实现(示例)1. 实体类2. Ma

电脑win32spl.dll文件丢失咋办? win32spl.dll丢失无法连接打印机修复技巧

《电脑win32spl.dll文件丢失咋办?win32spl.dll丢失无法连接打印机修复技巧》电脑突然提示win32spl.dll文件丢失,打印机死活连不上,今天就来给大家详细讲解一下这个问题的解... 不知道大家在使用电脑的时候是否遇到过关于win32spl.dll文件丢失的问题,win32spl.dl

Windows Server服务器上配置FileZilla后,FTP连接不上?

《WindowsServer服务器上配置FileZilla后,FTP连接不上?》WindowsServer服务器上配置FileZilla后,FTP连接错误和操作超时的问题,应该如何解决?首先,通过... 目录在Windohttp://www.chinasem.cnws防火墙开启的情况下,遇到的错误如下:无法与

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu

MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固 通俗易懂版)

《MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固通俗易懂版)》本文主要讲解了MySQL中的多表查询,包括子查询、笛卡尔积、自连接、多表查询的实现方法以及多列子查询等,通过实际例子和操... 目录复合查询1. 回顾查询基本操作group by 分组having1. 显示部门号为10的部门名,员

IDEA连接达梦数据库的详细配置指南

《IDEA连接达梦数据库的详细配置指南》达梦数据库(DMDatabase)作为国产关系型数据库的代表,广泛应用于企业级系统开发,本文将详细介绍如何在IntelliJIDEA中配置并连接达梦数据库,助力... 目录准备工作1. 下载达梦JDBC驱动配置步骤1. 将驱动添加到IDEA2. 创建数据库连接连接参数