Linux学习之MySQL连接查询

news2024/9/20 14:46:09

接上一篇

MySQL执行顺序

连接查询

连接查询也中多表查询,常用于查询来自于多张表的数据,通过不同的连接方式把多张表组成一张新的临时表,再对临时表做数据处理。

#表基础信息,内容可从上一篇博客中查看
mysql> desc departments;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| dept_id   | int         | NO   | PRI | NULL    | auto_increment |
| dept_name | varchar(10) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> desc employees;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| employee_id  | int         | NO   | PRI | NULL    | auto_increment |
| name         | varchar(10) | YES  |     | NULL    |                |
| hire_date    | date        | YES  |     | NULL    |                |
| birth_date   | date        | YES  |     | NULL    |                |
| email        | varchar(25) | YES  |     | NULL    |                |
| phone_number | char(11)    | YES  |     | NULL    |                |
| dept_id      | int         | YES  | MUL | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> desc salary;
+-------------+------+------+-----+---------+----------------+
| Field       | Type | Null | Key | Default | Extra          |
+-------------+------+------+-----+---------+----------------+
| id          | int  | NO   | PRI | NULL    | auto_increment |
| date        | date | YES  |     | NULL    |                |
| employee_id | int  | YES  | MUL | NULL    |                |
| basic       | int  | YES  |     | NULL    |                |
| bonus       | int  | YES  |     | NULL    |                |
+-------------+------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

内连接

#1.等值连接查询
#1)查询每个员工所在的部门名
mysql> select emp.name,dep.dept_name from employees emp,departments dep where emp.dept_id=dep.dept_id;
或
mysql> select emp.name,dep.dept_name from employees emp join departments dep on emp.dept_id=dep.dept_id;
#2) 查询员工编号8的员工所有部门的部门名称
mysql> select emp.name,dep.dept_name from employees emp,departments dep where emp.dept_id=dep.dept_id and emp.employee_id=8;
或
mysql> select emp.name,dep.dept_name from employees emp join departments dep on emp.dept_id=dep.dept_id and emp.employee_id=8;
#3)查询每个员工所有信息及所有部门名称
mysql> select emp.*,dep.dept_name from employees emp,departments dep where emp.dept_id=dep.dept_id;
或
mysql> select emp.*,dep.dept_name from employees emp join departments dep on emp.dept_id=dep.dept_id;
#4)查询每个员工姓名、部门编号、部门名称
mysql> select emp.name,dep.dept_id,dep.dept_name from employees emp,departments dep where emp.dept_id=dep.dept_id;
或
mysql> select emp.name,dep.dept_id,dep.dept_name from employees emp join departments dep on emp.dept_id=dep.dept_id;
#5)查询11号员工的名字及2018年每个月总工资
mysql> select emp.name,(sal.basic+sal.bonus) as 总工资 from employees emp join salary sal  on emp.employee_id=sal.employee_id where emp.employee_id=11 and year(date)=2018;
#6) 查询每个员工2018年的总工资
mysql> select emp.name,sum(sal.basic+sal.bonus) as 总工资 from employees emp join salary sal on emp.employee_id=sal.employee_id where year(date)=2018 group by name;
#7) 查询每个员工2018年的总工资,按总工资升序排列
mysql> select emp.name,sum(sal.basic+sal.bonus) as 总工资 from employees emp join salary sal on emp.employee_id=sal.employee_id where year(date)=2018 group by name order by 总工资;
#8) 查询2018年总工资大于30万的员工,按2018年总工资降序排列
mysql> select emp.name,sum(sal.basic+sal.bonus) as 总工资 from employees emp join salary sal on emp.employee_id=sal.employee_id where year(date)=2018 group by name having 总工资>300000 order by 总工资 desc;
#2.非等值连接查询
mysql> create table wage_grade(id int primary key auto_increment,grade char(1),low int,high int);
Query OK, 0 rows affected (0.85 sec)

mysql> insert into wage_grade(grade,low,high)values('A',5000,8000),('B', 8001, 10000),('C', 10001, 15000),
    -> ('D', 15001, 20000),('E', 20001, 1000000);
Query OK, 5 rows affected (0.08 sec)
Records: 5  Duplicates: 0  Warnings: 0
#1)查询2018年12月员工基本工资级别
mysql> select employee_id,date,basic,grade from salary as s inner join wage_grade as g on s.basic between g.low and g.high where year(date)=2018 and month(date)=12;
#2)查询2018年12月员工各基本工资级别的人数
mysql> select grade,count(grade) from salary as s inner join wage_grade as g on s.basic between g.low and g.high where year(date)=2018 and month(date)=12 group by grade;
#3)查询2018年12月员工基本工资级别,员工需要显示姓名
mysql> select emp.name,date,basic,grade from salary as s inner join wage_grade as g inner join employees emp on s.basic between g.low and g.high where year(date)=2018 and month(date)=12 and emp.employee_id=s.employee_id;

外连接

mysql> insert into departments(dept_name) values("小卖部"),("行政部"),("公关部");
#1. 左连接查询
#1)输出没有员工的部门名
mysql> select dept_name,emp.name from departments as dep left join employees emp on emp.dept_id=dep.dept_id where emp.name is null; 
#2. 右连接查询
mysql> insert  into  employees(name) values ("bob"),("tom"),("lily");
# 显示没有部门的员工名
mysql> select emp.name,dep.dept_name from departments dep right join employees emp on emp.dept_id=dep.dept_id where emp.dept_id is null;
#3. 全外连接查询
#1)输出2018年基本工资的最大值和最小值
mysql> (select basic from salary where year(date)=2018 order by basic desc limit 1) union (select basic from salary where year(date)=2018 order by basic limit 1);
#2)输出2018年1月10号基本工资的最大值和最小值
mysql> (select date , max(basic) as 工资 from salary where date=20180110)union(select date,min(basic) from salary where date=20180110);
#3)union all 不去重显示查询结果
mysql> (select employee_id , name , birth_date from employees where employee_id <= 5) union all (select employee_id , name , birth_date from employees where employee_id <= 6);

嵌套查询

嵌套查询:是指在一个完整的查询语句之中,包含若干个不同功能的小查询;从而一起完成复杂查询的一种编写形式。包含的查询放在()里 , 包含的查询出现的位置:

  • SELECT之后
  • FROM之后
  • WHERE
  • HAVING之后
#1. where之后嵌套查询
#1)查询运维部所有员工信息
mysql> select  *  from  employees  where  dept_id = (select dept_id from departments where dept_name="运维部");
#2)查询人事部2018年12月所有员工工资
mysql> select * from salary where year(date)=2018 and month(date)=12  and employee_id in (select employee_id from employees  where dept_id=(select dept_id from departments where dept_name='事部') );
#3)查询人事部和财务部员工信息
mysql> select dept_id , name  from employees  where dept_id in (  select dept_id from departments  where dept_name in ('人事部', '财务部')  );
#4)查询2018年12月所有比100号员工基本工资高的工资信息
mysql> select  *  from salary  where year(date)=2018 and month(date)=12 and  basic>(select basic from salary where year(date)=2018 and  month(date)=12 and employee_id=100);
#2. having之后嵌套查询
#查询部门员工总人数比开发部总人数少 的 部门名称和人数
mysql> select dept_id ,count(name) as total from employees group by dept_id  having  total < ( select count(name) from employees  where dept_id=( select dept_id from departments where dept_name='开发部') );
#3. from之后嵌套查询
#查询3号部门 、部门名称 及其部门内 员工的编号、名字 和 email
mysql> select dept_id, dept_name, employee_id, name, email  from ( select  d.dept_name, e.*  from departments as d  inner join employees as e  on d.dept_id=e.dept_id ) as tmp_table  where dept_id=3;
#4. select之后嵌套查询
#查询每个部门的人数: dept_id dept_name 部门人数
mysql> select  d.* , ( select count(name) from employees as e  where d.dept_id=e.dept_id) as 部门人数  from departments as d;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/988617.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

第15章_锁: (表级锁、页级锁、行锁、悲观锁、乐观锁、全局锁、死锁)

3.2 从数据操作的粒度划分&#xff1a;表级锁、页级锁、行锁 为了提高数据库并发度&#xff0c;每次锁定的数据范围越小越好&#xff0c;理论上每次只锁定当前操作的数据的方案会得到最大的并发度&#xff0c;但管理锁是很耗资源&#xff08;涉及获取、检查、释放锁等动作)。因…

我总结的《149个Python面试题.pdf》,都是干货!

大家好&#xff0c;我是涛哥。 很多小伙伴找Python面试资料&#xff0c;所以为了方便大家&#xff0c;涛哥我整理了《149个Python面试干货》&#xff0c;方便大家进行学习&#xff0c;尤其是要面试学习的同学可以重点学起来。 第一个部分就是讲Python基础相关内容 第二个部分…

JAVA毕业设计097—基于Java+Springboot+Vue+uniapp的医院挂号小程序系统(源码+数据库)

基于JavaSpringbootVueuniapp的医院挂号小程序系统(源码数据库)097 一、系统介绍 本系统前后端分离(网页端和小程序端都有) 本系统分为管理员、医院、用户三种角色(角色菜单可自行分配) 用户功能&#xff1a; 注册、登录、医院搜索、最新资讯、医生搜索、挂号预约、挂号记…

由于找不到msvcp120.dll无法继续执行代码,重新安装相关软件

在我们的生活中&#xff0c;计算机已经成为不可或缺的工具&#xff0c;我们依赖它来进行工作、学习和娱乐。然而&#xff0c;当我们在使用计算机时&#xff0c;有时会遭遇一些令人烦恼的问题&#xff0c;例如“找不到 msvcp120.dll 无法继续执行代码”的错误提示。这究竟是什么…

TGA格式文件转材质

今天淘宝上买了一个美女的模型&#xff0c;是blender的源文件&#xff0c;上面说有fbx格式的。我用unity&#xff0c;所以觉得应该可以用。文件内容如下图&#xff1a; FBX文件夹打开后&#xff0c;内容如下图所示&#xff0c;当时就预感到可能没有色彩。 unity打开后果然发现只…

Go 报错 Package libzmq was not found in the pkg-config search path.

make编译程序时&#xff0c;报错提示如下: 因为 zmq.h 是包含在开发包 libczmq-dev 中的&#xff0c;libzmq.pc 也是在 dev 包安装时才被导入&#xff0c;故需安装如下两个包&#xff1a; sudo apt install libzmq5 libczmq-dev

9月7日上课内容 redis群集

redis高可用重点回顾 redis的两种持久化方式 rdb 优缺点 缺点 ① 数据完整性不如AOF ② RDB类似于快照&#xff08;完备&#xff09; ③ 在进行备份时会阻塞进程 优点 ① 持久化的速度比较快&#xff08;因为保存的是数据结果&#xff09;&#xff0c;在写入到*.rdb持久化文…

C++内存泄露

目录 1.什么是内存泄露 2.内存泄露的危害 3.如何解决内存泄露等相关的问题 1.什么是内存泄露 在C/C中 &#xff0c;我们申请了资源&#xff0c;因为一些原因忘记对申请的资源进行释放&#xff0c;或者因为异常安全等问题没有进行释放就会造成内存泄露的。 2.内存泄露的危害…

火山引擎边缘云助力智能科技赋予生活更多新意

当下&#xff0c;先进的科学技术使得我们的日常生活变得快捷、舒适。大到上百层智能大厦、高端公共场所、社会智能基础设施&#xff0c;小到智能家居监控、指纹密码锁等&#xff0c;在这个充满想象力的时代&#xff0c;科技以更加智能化的方式改变和守护我们的生活。 引入智能…

【数据结构】树的基础入门

文章目录 什么是树树的常见术语树的表示树的应用 什么是树 相信大家刚学数据结构的时候最先接触的就是顺序表,栈,队列等线性结构. 而树则是一种非线性存储结构,存储的是具有“一对多”关系的数据元素的集合 非线性 体现在它是由n个有限结点(可以是零个结点)组成一个具有层次关…

Google云数据库的“Enterprise“和“Enterprise Plus“版怎么选

最近&#xff0c;Google Cloud SQL&#xff08;Google云上的RDS&#xff09;做了一次大的产品调整与发布&#xff1a;将原来的Cloud SQL分为了两个版本&#xff0c;分别为"Enterprise"和"Enterprise Plus"版本。本文概述了两个版本的异同&#xff0c;以帮助…

全解私域流量搭建细节剖析

一、找准吸粉引流的渠道和方式 引流思路&#xff1a; 引流方式&#xff1a;

微信协议开发

人微信号的二次开发可以包括但不限于以下方面&#xff1a; 自定义菜单&#xff1a;根据个人需求设置自定义&#xff0c;方便快速访问常用功能或链接。 消息管理&#xff1a;通过开发接口&#xff0c;实现消息的自动回复、关键词匹配等功能&#xff0c;提供更好的用户体验。 …

基础算法--理解递归

理解递归 递归的两个特点 调用自身结束条件 举个从小就听过的例子&#xff1a; 1. 从前有座山&#xff0c;山中有座庙&#xff0c;庙里有个老和尚&#xff0c;老和尚在给小和尚讲故事&#xff1a;2. 从前有座山&#xff0c;山中有座庙&#xff0c;庙里有个老和尚&#xff0c;…

unity scene场景调整好后让game窗口的视角与scene相同

调整scene中场景视角 选中相机 然后 如果要实现相反的功能 即scene的视角与game的一样则 选中相机

目标检测笔记(十四): 使用YOLOv8完成对图像的目标检测任务(从数据准备到训练测试部署的完整流程)

文章目录 一、目标检测介绍二、YOLOv8介绍三、源码获取四、环境搭建4.1 环境检测 五、数据集准备六、 模型训练6.1 方式一6.2 方式二6.3 针对其他任务 七、模型验证八、模型测试九、模型转换9.1 转onnx9.1.1 方式一 9.2 转tensorRT9.2.1 trtexec9.2.2 代码转换9.2.3 推理代码 一…

问道管理:炸裂上涨,“神奇力量”!Mate 60 Pro+来了

今天上午&#xff0c;同花顺软件刚增添的光刻机板块大爆发&#xff0c;光刻胶板块也大涨。早年两年的光伏&#xff0c;到本年上半年的光模块&#xff0c;再到最近的光刻胶、光刻机&#xff0c;股民评论&#xff1a;“光”&#xff0c;充溢奇特的力气。 上午收盘&#xff0c;上…

SQLite加密解密

Android 微信备份 微信聊天记录导出(2020新版) Android数据库加解密逆向分析&#xff08;三&#xff09;——微信数据库密码破解 微备份 论坛讨论 解密sqlite db数据库文件 转自windwos 安装 pysqlcipher3 下载 pysqlcipher 去https://pypi.org/search/?qpysqlcipher&…

Python并发编程实战,用多线程、多进程、多协程加速程序运行

文章目录 1. 并发 & 并行 、同步 & 异步1.1 并发 & 并行并发 Concurrency并行 Parallelism 1.2 同步 & 异步同步 Synchronous异步 Asynchronous 2. CPU密集型计算 & IO密集型计算2.1 CPU密集型&#xff08;CPU-bound&#xff09;2.2 IO密集型&#xff08;I…

智慧工地可视化解决方案-智慧工地源码

智慧工地是指运用信息化手段&#xff0c;围绕施工过程管理&#xff0c;建立互联协同、智能生产、科学管理的施工项目信息化生态圈&#xff0c;并将此数据在虚拟现实环境下与物联网采集到的工程信息进行数据挖掘分析&#xff0c;提供过程趋势预测及专家预案&#xff0c;实现工程…