当前位置: 首页 > news >正文

sql入门-多表查询

案例涉及表

 ----------------------------------建表语句之前翻看之前博客文章

 

 

多表查询

-- 学生表
create table studen (
    id int primary key auto_increment comment 'id',
    name varchar(50) comment '姓名',
    no varchar(10) comment '学号'
) comment '学生表';

insert into studen values (null , '黛绮丽','2000100101'),(null , '谢逊','2000100102'),(null , '殷天正','2000100103'),(null , '韦一笑','2000100184');

drop table studen;

use itheima;

-- 课程表
create table course(
    id int auto_increment primary key comment 'id',
    name varchar(10) comment '课程名称'
)comment '课程名称';

insert into course values (null,'java'),(null,'php'),(null,'mysql'),(null,'hadoop');

-- 中间表(连接两种表上的关系)
create table studen_course(
    id int primary key auto_increment comment 'id',
    studen_id int not null comment '学生id',
    course_id int not null comment '课程id',
    constraint wj_studen foreign key(studen_id) references studen(id),
     constraint wj_course foreign key(course_id) references course(id)
) comment '学生课程中间表';

insert into studen_course values(null,1,1),(null,1,2),(null,1,3),(null,1,4);

-- 多表查询需要消除笛卡尔积

-- 内连接    **--两张表交集部分
-- -- 隐式内连接  select 字段列表 表1,表2 where 条件


-- -- 显示内连接  select 字段列表 from 表1 [inner]join 被连接的表 on 连接条件
-- inner

-- 外连接  **-- 左外 相当于查询所有左表数据  并包含表1和表2 交集部分数据     右外同理
-- -- 左外连接  select 字段 from 表1 left[outer] join 表2 no 条件 ...
# 1.查询emp所有数据,和对应部门信息
select e.*,d.name from emp e left join dept d on e.det_id = d.id;

-- -- 右外连接  select 字段 from 表1 right[outer]join 表2 on 条件...
-- outer可以省略


-- 自连接查询  select 字段 from 表a 别名a join 表a 别名b on 条件...
# **-- 自连接查询,可以是内连接查询,也可以是外连接查询
#1. 查询员工并获取 及其领导的名字
select * from emp;

select e1.name 员工姓名 ,e1.age 年龄 , e2.name 领导名称  from emp e1 join emp e2 on e1.mangerid = e2.id;

#2. 查询员工并获取 及其领导的名字(没有领导也要显示出来) -- 使用左侧连接即可
select e1.name 员工姓名 ,e1.age 年龄 , e2.name 领导名称  from emp e1 left join emp e2 on e1.mangerid = e2.id;


-- 联合查询  select 字段 from 表a  unino[all] select 字段 from 表b ....
-- union 合并查询结果
-- union all 合并结果,并去重
# 1.薪资小于 5000 和 年龄大于50

-- union 会去重 猪八戒只出现一次
select * from emp where salary < 5000
union
select * from emp where age > 50;

-- 猪八戒 会重复出现两次 语法 union all 合并两次查询结果(不去重
select * from emp where salary < 5000  -- 猪八戒符合条件
union all
select * from emp where age > 50;  -- 猪八戒符合条件

-- 子查询  sql嵌套select 语句 称为嵌套查询 又称子查询
-- select * from t1 where column1 = (select column1 from t2)
-- 子查询外部可以是 inster update delete select任何一个

-- --  标量子查询(子查询结果为单个值)
# -- 常用的操作符  =等于  <>不等  >大于  >=大于等于 <小于  <=小于等于
# 查询销售部所有员工
select * from emp where det_id = (select id from dept where name = '销售部');

-- --  列子查询(查询结果为一列)
# -- 常用操作符 in指定范围内 not in指定范围外
# -- any子查询任意一个满足即可 some和any等同  all子查询返回列表所有值必须满足
# 查询比 销售部 所有人工资都高的员工信息
select id from dept where name = '销售部';
select salary from emp where det_id = (select id from dept where name = '销售部');
select * from emp where salary > all (select salary from emp where det_id = (select id from dept where name = '销售部'));

-- --  行子查询(查询结果为一行)
# -- 常用操作符 =  <>  in  not in
# 查询和 杨逍 的 薪资 及 直属领导 相同的员工
select salary , mangerid from emp where name = '杨逍' ;

select * from emp where (salary , mangerid) = (select salary , mangerid from emp where name = '杨逍');


-- -- 表子查询(查询结果为多行多列)
# 常用操作符  in
# 查询 宋远桥  和 鹿杖客 职位相同和薪资相同的员工
select salary , job from emp  where name = '宋远桥' || name = '鹿杖客';

select * from emp where (salary , job) in (select salary , job from emp  where name = '宋远桥' || name = '鹿杖客');

# 查询入职日期 '2006-01-01'之后的员工信息,及其部门信息
select id,det_id from emp where entrydate > '2006-01-01';
select e.* , d.name from emp e ,dept d where (e.id , d.id) in (select id,det_id from emp where entrydate > '2006-01-01');

select * from emp where entrydate > '2006-01-01';
select * from (select * from emp where entrydate > '2006-01-01') e left join emp d on e.det_id = d.id;

# -- -- 根据子查询位置,分为where之后,from之后,select之后


# ========================多表查询案例=======================
create table salgrade (
    grade int ,
    losal int ,
    hisal int
) comment '薪资等级表';

insert into salgrade values (1,0,3000);
insert into salgrade values (2,30001,5000);
insert into salgrade values (3,5001,8000);
insert into salgrade values (4,8001,10000);
insert into salgrade values (5,10001,15000);
insert into salgrade values (6,15001,20000);
insert into salgrade values (7,20001,25000);
insert into salgrade values (8,25001,30000);

# 1. 查询员工的姓名 年龄 职位  部门信息
select name , age , job ,det_id  from emp;
-- 隐式内连接--35ms
select e.name ,e.age ,e.job,d.name from dept d , emp e where e.det_id = d.id;
-- 显示外连接--28ms  外连接  左连接  查询交际部分-且返回左侧表所有
select e.name ,e.age ,e.job,d.name from  emp e left join dept d on d.id = e.det_id;



# 2.查询年龄小于30岁的员工姓名,年龄 职位,部门信息(显示内连接)
select * from emp where age < 30;
-- 隐式内连接
select e.name ,e.age ,e.job,d.name from emp e ,dept d where e.age < 30  and  e.det_id = d.id;
-- 显示内连接  -inner join  on   只查询交际部分
select * from emp e inner join dept d on e.det_id = d.id where e.age < 30;

# 3.查询所有员工的部门id,部门名称
-- distinct 去重
select distinct d.* from dept d , emp e where d.id = e.det_id;

# 4.查询所有年龄大于40岁的员工,及其部门的名称,如果员工没有部门,也需要展示出来
select e.* , d.name from emp e left join dept d on e.det_id = d.id where age > 40;

# 5.查询所有员工的工资的等级
select e.salary ,s.grade from emp e , salgrade s where e.salary between s.losal and s.hisal;

# 6.查询研发部所有员工信息及工资等级
select id from dept where name = '研发部';

select * from emp where det_id = (select id from dept where name = '研发部');
-- 子查询方法完成
select e.*, s.grade
from (select * from emp where det_id = (select id from dept where name = '研发部')) e,
     salgrade s
where e.salary between s.losal and s.hisal;

-- 内连接方法
select *
from emp e,
     dept d,
     salgrade s
where e.det_id = d.id
  and e.salary between s.losal and
    s.hisal
  and d.name = '研发部';

# 7.查询‘研发部员’ 工的 ’平均工资‘   -- 聚合函数 avg  平均值
select avg(e.salary)
from emp e ,dept d where e.det_id = d.id and d.name = '研发部';

# 8.查询工资比 '常遇春' 高的员工信息
select salary from emp where name = '常遇春';

select * from emp e where e.salary > (select salary from emp where name = '常遇春');

# 9.查询比平均工资高的员工信息
select avg(salary) from emp ;

select * from emp e where e.salary > (select avg(salary) from emp) ;

# 10.查询低于本部门平均工资的员工信息
-- a思路 ** 当前指定部门的平均薪资
select avg(salary) from emp where det_id = 1;

-- b  此处考察执行顺序  首先from e1   条件薪资小于  指定部门平均薪资
select e1.* , (select avg(salary) from emp where det_id = e1.det_id) 部门平均薪资
from emp e1
where e1.salary < (select avg(salary) from emp where det_id = e1.det_id);


# 11.查询所有的部门信息,并统计部门的员工人数
-- 获取指定部门统计人数
select count(*)
from emp where det_id = 1;
-- 统计部门人数
select d.name ,d.id ,(select count(*)from emp e where e.det_id = d.id ) 部门人数 from dept d;

# 12.查询所有学生 的选则课情况 展示学生名称 ,学号,课程名称
-- 涉及表 studen course stunden_course

select s.name, s.no, c.name
from studen s,
     studen_course sc,
     course c
where s.id = sc.studen_id
  and c.id = sc.course_id;

相关文章:

sql入门-多表查询

案例涉及表 ----------------------------------建表语句之前翻看之前博客文章 多表查询 -- 学生表 create table studen ( id int primary key auto_increment comment id, name varchar(50) comment 姓名, no varchar(10) comment 学号 ) comment 学生表; insert…...

软考A计划-网络工程师-必考知识点-上

点击跳转专栏>Unity3D特效百例点击跳转专栏>案例项目实战源码点击跳转专栏>游戏脚本-辅助自动化点击跳转专栏>Android控件全解手册点击跳转专栏>Scratch编程案例点击跳转>软考全系列点击跳转>蓝桥系列 &#x1f449;关于作者 专注于Android/Unity和各种游…...

kafka复习:(17)seekToBeginning的用法

从分区的开始进行消费&#xff0c;因为kafka会定期清理历史数据&#xff0c;所以分区开始的位移不一定为0。seekToBeginning只是从目前保留的数据中最小的offset进行消费 package com.cisdi.dsp.modules.metaAnalysis.rest.kafka2023;import org.apache.kafka.clients.consume…...

C# textBox1.Text=““与textBox1.Clear()的区别

一、区别 textbox.Text "" 和 textbox.Clear() 都可以用于清空文本框的内容&#xff0c;但它们之间有一些细微的区别。 textbox.Text "": 这种方式会将文本框的 Text 属性直接设置为空字符串。这样会立即清除文本框的内容&#xff0c;并将文本框显示为空…...

CnetSDK .NET OCR SDK Crack

CnetSDK .NET OCR SDK Crack CnetSDK.NET OCR库SDK是一款高度准确的.NET OCR扫描仪软件&#xff0c;用于使用手写、文本和其他符号等图像进行字符识别。它是一款.NET OCR库软件&#xff0c;使用Tesseract OCR引擎技术&#xff0c;可将字符识别准确率提高99%。通过将此.NET OCR扫…...

Python最新面试题汇总及答案

一、基础部分 1、什么是Python&#xff1f;为什么它会如此流行&#xff1f;Python是一种解释的、高级的、通用的编程语言。Python的设计理念是通过使用必要的空格与空行&#xff0c;增强代码的可读性。它之所以受欢迎&#xff0c;就是因为它具有简单易用的语法 2、为什么Pytho…...

设计模式(单例模式,工厂模式),线程池

目录 什么是设计模式? 单例模式 饿汉模式 懒汉模式 工厂模式 线程池 线程池种类 ThreadPoolExcutor的构造方法: 手动实现一个线程池 什么是设计模式? 计算机行业程序员水平层次不齐,为了让所有人都能够写出规范的代码,于是就有了设计模式,针对一些典型的场景,给出一…...

在mybatis中的mapper.xml中如何使用parameterType实现方法单个传参,对象传参,多参数传参.

在MyBatis的mapper.xml文件中&#xff0c;可以使用parameterType属性来指定方法的参数类型。parameterType属性用于指定传递给映射方法的参数类型&#xff0c;这将影响到MyBatis在映射方法执行时如何处理参数。 以下是三种不同情况下如何在mapper.xml中使用parameterType实现方…...

No120.精选前端面试题,享受每天的挑战和学习

文章目录 浏览器强制缓存和协商缓存cookie&#xff0c;localStorage、sessionStoragejs闭包&#xff0c;原型&#xff0c;原型链箭头函数和普通函数的区别promise的状态扭转 浏览器强制缓存和协商缓存 浏览器缓存是浏览器用于提高网页加载速度的一种机制。浏览器缓存分为强制缓…...

c# 访问sqlServer数据库时的连接字符串

//sql server 身份验证的场合&#xff0c; 连接字符串 private string ConnstrSqlServer "server服务器名称;uid登录名称;pwd登录密码;database数据库名称"; //windows 身份验证连接字符串 private string ConnstrWindows "server服务器名称;database数据库…...

排序算法概述

1.排序算法分类 **比较类算法排序&#xff1a;**通过比较来决定元素的时间复杂度的相对次序&#xff0c;由于其时间复杂度不能突破 O ( n l o g n ) O(nlogn) O(nlogn)&#xff0c;因此也称为非线性时间比较类算法 **非比较类算法排序&#xff1a;**不通过比较来决定元素间的…...

ChatGPT在高等教育中的应用利弊探讨

​人工智能在教育领域的应用日益广泛。2022年11月OpenAI开发的聊天机器人ChatGPT在全球范围内流传开来&#xff0c;其中用户数量最多的国家是美国(15.22%)。由于ChatGPT应用广泛&#xff0c;具有类似人类回答问题的能力&#xff0c;它正在成为许多学生和教育工作者的可信赖伙伴…...

Java之API详解之Runtime的详细解析

3.1 概述 Runtime表示Java中运行时对象&#xff0c;可以获取到程序运行时设计到的一些信息 3.2 常见方法 常见方法介绍 我们要学习的Object类中的常见方法如下所示&#xff1a; public static Runtime getRuntime() //当前系统的运行环境对象 public void exit(int statu…...

机器学习之softmax

Softmax是一个常用于多类别分类问题的激活函数和归一化方法。它将一个向量的原始分数&#xff08;也称为 logits&#xff09;转换为概率分布&#xff0c;使得每个类别的概率值在0到1之间&#xff0c;同时确保所有类别的概率之和等于1。Softmax函数的定义如下&#xff1a; 对于…...

npm script命令

1 串行/并行执行命令 //串行 npm-run-all text test npm run text && npm run test //并行改成& npm-run-all --parallel text test npm run text & npm run test2 传递参数 {"lint": "eslint js/*.js","lint:fix"&#xff1a…...

【力扣周赛】第360场周赛

【力扣周赛】第360场周赛 8015.距离原点最远的点题目描述解题思路 8022. 找出美丽数组的最小和题目描述解题思路 8015.距离原点最远的点 题目描述 描述&#xff1a;给你一个长度为 n 的字符串 moves &#xff0c;该字符串仅由字符 ‘L’、‘R’ 和 ‘_’ 组成。字符串表示你在…...

php环境变量的配置步骤

要配置PHP的环境变量&#xff0c;以便在命令行中直接使用php命令&#xff0c;以下是一般的步骤&#xff1a; Windows 操作系统 下载和安装PHP&#xff1a;首先&#xff0c;你需要从PHP官方网站&#xff08;https://www.php.net/downloads.php&#xff09;下载适用于你的操作系…...

Kdtree

Kdtree kdtree 就是在 n 维空间对数据点进行二分&#xff1b;具体先确定一个根&#xff0c;然后小于在这个维度上的根的节点在左边&#xff0c;大于的在右边&#xff0c;再进行下一个维度的划分。直到维度结束&#xff0c;再重复&#xff0c;或者直到达到了结束条件&#xff1…...

算法leetcode|74. 搜索二维矩阵(rust重拳出击)

文章目录 74. 搜索二维矩阵&#xff1a;样例 1&#xff1a;样例 2&#xff1a;提示&#xff1a; 分析&#xff1a;题解&#xff1a;rust&#xff1a;go&#xff1a;c&#xff1a;python&#xff1a;java&#xff1a; 74. 搜索二维矩阵&#xff1a; 给你一个满足下述两条属性的…...

element浅尝辄止7:InfiniteScroll 无限滚动

滚动加载&#xff1a;滚动至底部时&#xff0c;加载更多数据。 1.如何使用&#xff1f; //在要实现滚动加载的列表上上添加v-infinite-scroll&#xff0c;并赋值相应的加载方法&#xff0c; //可实现滚动到底部时自动执行加载方法。<template><ul class"infinit…...

Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误

HTTP 状态码 406 (Not Acceptable) 和 500 (Internal Server Error) 是两类完全不同的错误&#xff0c;它们的含义、原因和解决方法都有显著区别。以下是详细对比&#xff1a; 1. HTTP 406 (Not Acceptable) 含义&#xff1a; 客户端请求的内容类型与服务器支持的内容类型不匹…...

docker详细操作--未完待续

docker介绍 docker官网: Docker&#xff1a;加速容器应用程序开发 harbor官网&#xff1a;Harbor - Harbor 中文 使用docker加速器: Docker镜像极速下载服务 - 毫秒镜像 是什么 Docker 是一种开源的容器化平台&#xff0c;用于将应用程序及其依赖项&#xff08;如库、运行时环…...

从WWDC看苹果产品发展的规律

WWDC 是苹果公司一年一度面向全球开发者的盛会&#xff0c;其主题演讲展现了苹果在产品设计、技术路线、用户体验和生态系统构建上的核心理念与演进脉络。我们借助 ChatGPT Deep Research 工具&#xff0c;对过去十年 WWDC 主题演讲内容进行了系统化分析&#xff0c;形成了这份…...

1688商品列表API与其他数据源的对接思路

将1688商品列表API与其他数据源对接时&#xff0c;需结合业务场景设计数据流转链路&#xff0c;重点关注数据格式兼容性、接口调用频率控制及数据一致性维护。以下是具体对接思路及关键技术点&#xff1a; 一、核心对接场景与目标 商品数据同步 场景&#xff1a;将1688商品信息…...

Mac软件卸载指南,简单易懂!

刚和Adobe分手&#xff0c;它却总在Library里给你写"回忆录"&#xff1f;卸载的Final Cut Pro像电子幽灵般阴魂不散&#xff1f;总是会有残留文件&#xff0c;别慌&#xff01;这份Mac软件卸载指南&#xff0c;将用最硬核的方式教你"数字分手术"&#xff0…...

数据库分批入库

今天在工作中&#xff0c;遇到一个问题&#xff0c;就是分批查询的时候&#xff0c;由于批次过大导致出现了一些问题&#xff0c;一下是问题描述和解决方案&#xff1a; 示例&#xff1a; // 假设已有数据列表 dataList 和 PreparedStatement pstmt int batchSize 1000; // …...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建

华为云FlexusDeepSeek征文&#xff5c;DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色&#xff0c;华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型&#xff0c;能助力我们轻松驾驭 DeepSeek-V3/R1&#xff0c;本文中将分享如何…...

select、poll、epoll 与 Reactor 模式

在高并发网络编程领域&#xff0c;高效处理大量连接和 I/O 事件是系统性能的关键。select、poll、epoll 作为 I/O 多路复用技术的代表&#xff0c;以及基于它们实现的 Reactor 模式&#xff0c;为开发者提供了强大的工具。本文将深入探讨这些技术的底层原理、优缺点。​ 一、I…...

SpringTask-03.入门案例

一.入门案例 启动类&#xff1a; package com.sky;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCach…...

九天毕昇深度学习平台 | 如何安装库?

pip install 库名 -i https://pypi.tuna.tsinghua.edu.cn/simple --user 举个例子&#xff1a; 报错 ModuleNotFoundError: No module named torch 那么我需要安装 torch pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple --user pip install 库名&#x…...