DBA 数据库管理 . 内连接.外链接

内连接

通过链接把多张表组合成一张临时表

select  表头名  from  表名1 inner join 表名2 ;

 select * from employees inner join departments

select  表头名  from  表名1 inner join 表名2    on   链接条件;

select * from employees inner join departments on employees.dept_id = departments.dept_id;
在临时组成新表里,可以在哟以下处理

select  表头名  from  表名1 inner join 表名2    on   链接条件[where | order by | group by|  limit | having];

等值链接 : 前提是链接里有储存相同数据的表头

select * from employees inner join departments on employees.dept_id = departments.dept_id;

定义别名后必须使用别名表示表名

  1. select e.* , d.dept_name
  2. from employees as e inner join departments as d on e.dept_id=d.dept_id;

查询每个员工2018年的总工资

select name ,sum(basic+bonus) as total from employees inner join salary on employees.employee_id = salary.employee_id where year(date)=2018 group by name;

select salary.employee_id ,sum(basic+bonus) as total from employees inner join salary on employees.employee_id = salary.employee_id  where year(date)=2018 group by employee_id having total > 300000 order by total desc ,employee_id asc;查询2018年总工资大于30万的员工,按2018年总工资降序排列

非等值链接: 表里没有存储相同数据的表头

select employee_id ,basic,garde from salary inner join wage_grade on salary.basic between wage_grade.low and wage_grade.high where year(date)=2018 and month(date)=12;
查询2018年12月员工基本工资

select garde as 级别,count(employee_id) as 总人数 from salary inner join wage_grade on salary.basic between wage_grade.low and wage_grade.high where year(date)=2018 and month(date)=12 group by garde;查询2018年12月员工基本工资级别的人数

3张表连接的例子

select name ,basic ,garde from employees inner join salary on employees.employee_id  = salary.employee_id  inner join wage_grade on salary.basic between wage_grade.low and wage_grade.high where year(date)=2018 and month(date)=12;

外链接查询

外链接是比较2个表的不同

左链接 用左表数据和右表数据作比较  输出结果左表头数据全部显示,

右边仅显示与链接匹配的`行

select  表头名  from  表名1   left jion  表名2  on 链接条件  ;

select  表头名  from  表名1   left jion  表名2  on 链接条件 ·[where |group by| orderby | having | limit] ;

 select dept_name, name from departments left join employees on departments.dept_id=employees.dept_id where name is null;
 

右链接 用右表数据和左表数据作比较  输出结果右表头数据全部显示

右边仅显示与链接匹配的`行

select  表头名  from  表名1   right  jion  表名2  on 链接条件 ·[where |group by| orderby | having | limit] ;

mysql> select dept_name,name from departments as d right join employees as e on d.dept_id=e.dept_id where dept_name is null;

update employees set dept_id=11 where name="bob"; 

全链接查询

把多个select命令查询的行一起数据  多个select查询,表头数要一致

去重行的输出

mysql> (select date , max(basic) as 工资 from salary where date=20180110)union(select date,min(basic) from salary where date=20180110);

(select 查询命令)union  (select 查询命令);

不去重行的输出

select 查询命令)union  all(select 查询命令);

嵌套查询

select 查询命令包含select查询命令

where之后嵌套查询  分组

select  表头名  from 库.表  where  表头名   判断符号 ( select  查询命令)

select employee_id,date,basic,bonus 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);


having之后嵌套查询    把()里的查询结果作为过滤条件,

select  表头名  from 库.表  having  表头名   判断符号 ( select  查询命令) 

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='开发部')

from之后嵌套查询   把()的查询结果作表使用

select  表头名  from  (select 查询结果)as 临时表名 where 筛选条件;

select employee_id ,name,email,dept_name 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;

select之后嵌套查询

select  表头名, (select 查询命令)as 表头名  from 库.表 where 筛选条件;

select d.* ,(select count(name)from employees as e where d.dept_id=e.dept_id)as vumen from departments as d;

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 06:12:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 06:12:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 06:12:03       58 阅读
  4. Python语言-面向对象

    2024-07-12 06:12:03       69 阅读

热门阅读

  1. CentOS搭建FTP服务器教程

    2024-07-12 06:12:03       26 阅读
  2. 自动优化:SQL Server数据库自动收缩配置指南

    2024-07-12 06:12:03       27 阅读
  3. 数据结构第20节 快速排序以及优化

    2024-07-12 06:12:03       27 阅读
  4. 力扣 1两数之和

    2024-07-12 06:12:03       27 阅读
  5. Elasticsearch基础教程

    2024-07-12 06:12:03       22 阅读
  6. 分享WPF的UI开源库

    2024-07-12 06:12:03       29 阅读
  7. C# —— 数组的告诫查询方法

    2024-07-12 06:12:03       26 阅读
  8. 内网、外网通信的底层实现

    2024-07-12 06:12:03       23 阅读
  9. 什么是XSS跨站脚本攻击

    2024-07-12 06:12:03       25 阅读
  10. 托管你的程序——命令行后台运行记录

    2024-07-12 06:12:03       27 阅读
  11. JDBC 实例分享——简易图书管理系统

    2024-07-12 06:12:03       25 阅读
  12. 工作理念分享

    2024-07-12 06:12:03       26 阅读
  13. 如何安装和管理RabbitMQ

    2024-07-12 06:12:03       29 阅读
  14. 微信小程序连接阿里云IOT物联网平台

    2024-07-12 06:12:03       28 阅读