当前位置: 首页 > 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…...

django filter 统计数量 按属性去重

在Django中&#xff0c;如果你想要根据某个属性对查询集进行去重并统计数量&#xff0c;你可以使用values()方法配合annotate()方法来实现。这里有两种常见的方法来完成这个需求&#xff1a; 方法1&#xff1a;使用annotate()和Count 假设你有一个模型Item&#xff0c;并且你想…...

基于Docker Compose部署Java微服务项目

一. 创建根项目 根项目&#xff08;父项目&#xff09;主要用于依赖管理 一些需要注意的点&#xff1a; 打包方式需要为 pom<modules>里需要注册子模块不要引入maven的打包插件&#xff0c;否则打包时会出问题 <?xml version"1.0" encoding"UTF-8…...

QT: `long long` 类型转换为 `QString` 2025.6.5

在 Qt 中&#xff0c;将 long long 类型转换为 QString 可以通过以下两种常用方法实现&#xff1a; 方法 1&#xff1a;使用 QString::number() 直接调用 QString 的静态方法 number()&#xff0c;将数值转换为字符串&#xff1a; long long value 1234567890123456789LL; …...

力扣-35.搜索插入位置

题目描述 给定一个排序数组和一个目标值&#xff0c;在数组中找到目标值&#xff0c;并返回其索引。如果目标值不存在于数组中&#xff0c;返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...

Python Ovito统计金刚石结构数量

大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...

Java数值运算常见陷阱与规避方法

整数除法中的舍入问题 问题现象 当开发者预期进行浮点除法却误用整数除法时,会出现小数部分被截断的情况。典型错误模式如下: void process(int value) {double half = value / 2; // 整数除法导致截断// 使用half变量 }此时...

tomcat指定使用的jdk版本

说明 有时候需要对tomcat配置指定的jdk版本号&#xff0c;此时&#xff0c;我们可以通过以下方式进行配置 设置方式 找到tomcat的bin目录中的setclasspath.bat。如果是linux系统则是setclasspath.sh set JAVA_HOMEC:\Program Files\Java\jdk8 set JRE_HOMEC:\Program Files…...

消防一体化安全管控平台:构建消防“一张图”和APP统一管理

在城市的某个角落&#xff0c;一场突如其来的火灾打破了平静。熊熊烈火迅速蔓延&#xff0c;滚滚浓烟弥漫开来&#xff0c;周围群众的生命财产安全受到严重威胁。就在这千钧一发之际&#xff0c;消防救援队伍迅速行动&#xff0c;而豪越科技消防一体化安全管控平台构建的消防“…...

PH热榜 | 2025-06-08

1. Thiings 标语&#xff1a;一套超过1900个免费AI生成的3D图标集合 介绍&#xff1a;Thiings是一个不断扩展的免费AI生成3D图标库&#xff0c;目前已有超过1900个图标。你可以按照主题浏览&#xff0c;生成自己的图标&#xff0c;或者下载整个图标集。所有图标都可以在个人或…...

FOPLP vs CoWoS

以下是 FOPLP&#xff08;Fan-out panel-level packaging 扇出型面板级封装&#xff09;与 CoWoS&#xff08;Chip on Wafer on Substrate&#xff09;两种先进封装技术的详细对比分析&#xff0c;涵盖技术原理、性能、成本、应用场景及市场趋势等维度&#xff1a; 一、技术原…...