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编程案例点击跳转>软考全系列点击跳转>蓝桥系列 👉关于作者 专注于Android/Unity和各种游…...
kafka复习:(17)seekToBeginning的用法
从分区的开始进行消费,因为kafka会定期清理历史数据,所以分区开始的位移不一定为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() 都可以用于清空文本框的内容,但它们之间有一些细微的区别。 textbox.Text "": 这种方式会将文本框的 Text 属性直接设置为空字符串。这样会立即清除文本框的内容,并将文本框显示为空…...

CnetSDK .NET OCR SDK Crack
CnetSDK .NET OCR SDK Crack CnetSDK.NET OCR库SDK是一款高度准确的.NET OCR扫描仪软件,用于使用手写、文本和其他符号等图像进行字符识别。它是一款.NET OCR库软件,使用Tesseract OCR引擎技术,可将字符识别准确率提高99%。通过将此.NET OCR扫…...
Python最新面试题汇总及答案
一、基础部分 1、什么是Python?为什么它会如此流行?Python是一种解释的、高级的、通用的编程语言。Python的设计理念是通过使用必要的空格与空行,增强代码的可读性。它之所以受欢迎,就是因为它具有简单易用的语法 2、为什么Pytho…...

设计模式(单例模式,工厂模式),线程池
目录 什么是设计模式? 单例模式 饿汉模式 懒汉模式 工厂模式 线程池 线程池种类 ThreadPoolExcutor的构造方法: 手动实现一个线程池 什么是设计模式? 计算机行业程序员水平层次不齐,为了让所有人都能够写出规范的代码,于是就有了设计模式,针对一些典型的场景,给出一…...
在mybatis中的mapper.xml中如何使用parameterType实现方法单个传参,对象传参,多参数传参.
在MyBatis的mapper.xml文件中,可以使用parameterType属性来指定方法的参数类型。parameterType属性用于指定传递给映射方法的参数类型,这将影响到MyBatis在映射方法执行时如何处理参数。 以下是三种不同情况下如何在mapper.xml中使用parameterType实现方…...

No120.精选前端面试题,享受每天的挑战和学习
文章目录 浏览器强制缓存和协商缓存cookie,localStorage、sessionStoragejs闭包,原型,原型链箭头函数和普通函数的区别promise的状态扭转 浏览器强制缓存和协商缓存 浏览器缓存是浏览器用于提高网页加载速度的一种机制。浏览器缓存分为强制缓…...
c# 访问sqlServer数据库时的连接字符串
//sql server 身份验证的场合, 连接字符串 private string ConnstrSqlServer "server服务器名称;uid登录名称;pwd登录密码;database数据库名称"; //windows 身份验证连接字符串 private string ConnstrWindows "server服务器名称;database数据库…...
排序算法概述
1.排序算法分类 **比较类算法排序:**通过比较来决定元素的时间复杂度的相对次序,由于其时间复杂度不能突破 O ( n l o g n ) O(nlogn) O(nlogn),因此也称为非线性时间比较类算法 **非比较类算法排序:**不通过比较来决定元素间的…...

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

Java之API详解之Runtime的详细解析
3.1 概述 Runtime表示Java中运行时对象,可以获取到程序运行时设计到的一些信息 3.2 常见方法 常见方法介绍 我们要学习的Object类中的常见方法如下所示: public static Runtime getRuntime() //当前系统的运行环境对象 public void exit(int statu…...
机器学习之softmax
Softmax是一个常用于多类别分类问题的激活函数和归一化方法。它将一个向量的原始分数(也称为 logits)转换为概率分布,使得每个类别的概率值在0到1之间,同时确保所有类别的概率之和等于1。Softmax函数的定义如下: 对于…...
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":…...
【力扣周赛】第360场周赛
【力扣周赛】第360场周赛 8015.距离原点最远的点题目描述解题思路 8022. 找出美丽数组的最小和题目描述解题思路 8015.距离原点最远的点 题目描述 描述:给你一个长度为 n 的字符串 moves ,该字符串仅由字符 ‘L’、‘R’ 和 ‘_’ 组成。字符串表示你在…...
php环境变量的配置步骤
要配置PHP的环境变量,以便在命令行中直接使用php命令,以下是一般的步骤: Windows 操作系统 下载和安装PHP:首先,你需要从PHP官方网站(https://www.php.net/downloads.php)下载适用于你的操作系…...
Kdtree
Kdtree kdtree 就是在 n 维空间对数据点进行二分;具体先确定一个根,然后小于在这个维度上的根的节点在左边,大于的在右边,再进行下一个维度的划分。直到维度结束,再重复,或者直到达到了结束条件࿱…...

算法leetcode|74. 搜索二维矩阵(rust重拳出击)
文章目录 74. 搜索二维矩阵:样例 1:样例 2:提示: 分析:题解:rust:go:c:python:java: 74. 搜索二维矩阵: 给你一个满足下述两条属性的…...
element浅尝辄止7:InfiniteScroll 无限滚动
滚动加载:滚动至底部时,加载更多数据。 1.如何使用? //在要实现滚动加载的列表上上添加v-infinite-scroll,并赋值相应的加载方法, //可实现滚动到底部时自动执行加载方法。<template><ul class"infinit…...

Linux应用开发之网络套接字编程(实例篇)
服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …...

Chapter03-Authentication vulnerabilities
文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

Docker 离线安装指南
参考文章 1、确认操作系统类型及内核版本 Docker依赖于Linux内核的一些特性,不同版本的Docker对内核版本有不同要求。例如,Docker 17.06及之后的版本通常需要Linux内核3.10及以上版本,Docker17.09及更高版本对应Linux内核4.9.x及更高版本。…...
HTML 语义化
目录 HTML 语义化HTML5 新特性HTML 语义化的好处语义化标签的使用场景最佳实践 HTML 语义化 HTML5 新特性 标准答案: 语义化标签: <header>:页头<nav>:导航<main>:主要内容<article>&#x…...

iOS 26 携众系统重磅更新,但“苹果智能”仍与国行无缘
美国西海岸的夏天,再次被苹果点燃。一年一度的全球开发者大会 WWDC25 如期而至,这不仅是开发者的盛宴,更是全球数亿苹果用户翘首以盼的科技春晚。今年,苹果依旧为我们带来了全家桶式的系统更新,包括 iOS 26、iPadOS 26…...

LeetCode - 394. 字符串解码
题目 394. 字符串解码 - 力扣(LeetCode) 思路 使用两个栈:一个存储重复次数,一个存储字符串 遍历输入字符串: 数字处理:遇到数字时,累积计算重复次数左括号处理:保存当前状态&a…...

STM32F4基本定时器使用和原理详解
STM32F4基本定时器使用和原理详解 前言如何确定定时器挂载在哪条时钟线上配置及使用方法参数配置PrescalerCounter ModeCounter Periodauto-reload preloadTrigger Event Selection 中断配置生成的代码及使用方法初始化代码基本定时器触发DCA或者ADC的代码讲解中断代码定时启动…...

P3 QT项目----记事本(3.8)
3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...

HashMap中的put方法执行流程(流程图)
1 put操作整体流程 HashMap 的 put 操作是其最核心的功能之一。在 JDK 1.8 及以后版本中,其主要逻辑封装在 putVal 这个内部方法中。整个过程大致如下: 初始判断与哈希计算: 首先,putVal 方法会检查当前的 table(也就…...

VM虚拟机网络配置(ubuntu24桥接模式):配置静态IP
编辑-虚拟网络编辑器-更改设置 选择桥接模式,然后找到相应的网卡(可以查看自己本机的网络连接) windows连接的网络点击查看属性 编辑虚拟机设置更改网络配置,选择刚才配置的桥接模式 静态ip设置: 我用的ubuntu24桌…...