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

内外连接【MySQL】

文章目录

  • 表的内外连接
  • 内连接
  • 外连接

表的内外连接

显示SMITH的名字和部门名称

内连接

第一种写法,用笛卡尔积

mysql> select emp.ename,dept.dname from emp,dept where emp.deptno=dept.deptno ;
+--------+------------+
| ename  | dname      |
+--------+------------+
| SMITH  | RESEARCH   |
| ALLEN  | SALES      |
| WARD   | SALES      |
| JONES  | RESEARCH   |
| MARTIN | SALES      |
| BLAKE  | SALES      |
| CLARK  | ACCOUNTING |
| SCOTT  | RESEARCH   |
| KING   | ACCOUNTING |
| TURNER | SALES      |
| ADAMS  | RESEARCH   |
| JAMES  | SALES      |
| FORD   | RESEARCH   |
| MILLER | ACCOUNTING |
+--------+------------+
14 rows in set (0.00 sec)

内连接

mysql> select ename,dname  from emp inner join dept on emp.deptno=dept.deptno ;
+--------+------------+
| ename  | dname      |
+--------+------------+
| SMITH  | RESEARCH   |
| ALLEN  | SALES      |
| WARD   | SALES      |
| JONES  | RESEARCH   |
| MARTIN | SALES      |
| BLAKE  | SALES      |
| CLARK  | ACCOUNTING |
| SCOTT  | RESEARCH   |
| KING   | ACCOUNTING |
| TURNER | SALES      |
| ADAMS  | RESEARCH   |
| JAMES  | SALES      |
| FORD   | RESEARCH   |
| MILLER | ACCOUNTING |
+--------+------------+
14 rows in set (0.00 sec)

mysql> select ename,dname  from emp inner join dept on emp.deptno=dept.deptno where ename='SMITH';
+-------+----------+
| ename | dname    |
+-------+----------+
| SMITH | RESEARCH |
+-------+----------+
1 row in set (0.00 sec)


外连接

create table stu (id int, name varchar(30)); -- 学生表
insert into stu values(1,'jack'),(2,'tom'),(3,'kity'),(4,'nono');
create table exam (id int, grade int); -- 成绩表
insert into exam values(1, 56),(2,76),(11, 8);

例如:查询所有学生的成绩,如果这个学生没有成绩,也要将学生的个人信息显示出来

左外连接:如果联合查询,左侧的表完全显示,右侧的表按条件拼接,条件满足,拼接上,若条件不满足,拼接NULL

mysql> select * from stu left join exam on stu.id=exam.id;
+------+------+------+-------+
| id   | name | id   | grade |
+------+------+------+-------+
|    1 | jack |    1 |    56 |
|    2 | tom  |    2 |    76 |
|    3 | kity | NULL |  NULL |
|    4 | nono | NULL |  NULL |
+------+------+------+-------+
4 rows in set (0.00 sec)

右外连接
如果联合查询,右侧的表完全显示我们就说是右外连接

例如:对stu表和exam表联合查询,把所有的成绩都显示出来,即使这个成绩没有学生与它对应,也要
显示出来

mysql> select * from stu right join exam on stu.id=exam.id;
+------+------+------+-------+
| id   | name | id   | grade |
+------+------+------+-------+
|    1 | jack |    1 |    56 |
|    2 | tom  |    2 |    76 |
| NULL | NULL |   11 |     8 |
+------+------+------+-------+
3 rows in set (0.00 sec)


列出部门名称和这些部门的员工信息,同时列出没有员工的部门

1、左外连接

mysql> select * from dept left join  emp on dept.deptno=emp.deptno;
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
| deptno | dname      | loc      | empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno |
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
|     20 | RESEARCH   | DALLAS   | 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 |  800.00 |    NULL |     20 |
|     30 | SALES      | CHICAGO  | 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 |  300.00 |     30 |
|     30 | SALES      | CHICAGO  | 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 |  500.00 |     30 |
|     20 | RESEARCH   | DALLAS   | 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 |    NULL |     20 |
|     30 | SALES      | CHICAGO  | 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |
|     30 | SALES      | CHICAGO  | 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 |    NULL |     30 |
|     10 | ACCOUNTING | NEW YORK | 007782 | CLARK  | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 |    NULL |     10 |
|     20 | RESEARCH   | DALLAS   | 007788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 |    NULL |     20 |
|     10 | ACCOUNTING | NEW YORK | 007839 | KING   | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |    NULL |     10 |
|     30 | SALES      | CHICAGO  | 007844 | TURNER | SALESMAN  | 7698 | 1981-09-08 00:00:00 | 1500.00 |    0.00 |     30 |
|     20 | RESEARCH   | DALLAS   | 007876 | ADAMS  | CLERK     | 7788 | 1987-05-23 00:00:00 | 1100.00 |    NULL |     20 |
|     30 | SALES      | CHICAGO  | 007900 | JAMES  | CLERK     | 7698 | 1981-12-03 00:00:00 |  950.00 |    NULL |     30 |
|     20 | RESEARCH   | DALLAS   | 007902 | FORD   | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 |    NULL |     20 |
|     10 | ACCOUNTING | NEW YORK | 007934 | MILLER | CLERK     | 7782 | 1982-01-23 00:00:00 | 1300.00 |    NULL |     10 |
|     40 | OPERATIONS | BOSTON   |   NULL | NULL   | NULL      | NULL | NULL                |    NULL |    NULL |   NULL |
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
15 rows in set (0.01 sec)




mysql> select * from dept left join  emp on dept.deptno=emp.deptno  order by dept.deptno asc;
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
| deptno | dname      | loc      | empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno |
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
|     10 | ACCOUNTING | NEW YORK | 007782 | CLARK  | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 |    NULL |     10 |
|     10 | ACCOUNTING | NEW YORK | 007839 | KING   | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |    NULL |     10 |
|     10 | ACCOUNTING | NEW YORK | 007934 | MILLER | CLERK     | 7782 | 1982-01-23 00:00:00 | 1300.00 |    NULL |     10 |
|     20 | RESEARCH   | DALLAS   | 007902 | FORD   | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 |    NULL |     20 |
|     20 | RESEARCH   | DALLAS   | 007788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 |    NULL |     20 |
|     20 | RESEARCH   | DALLAS   | 007876 | ADAMS  | CLERK     | 7788 | 1987-05-23 00:00:00 | 1100.00 |    NULL |     20 |
|     20 | RESEARCH   | DALLAS   | 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 |  800.00 |    NULL |     20 |
|     20 | RESEARCH   | DALLAS   | 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 |    NULL |     20 |
|     30 | SALES      | CHICAGO  | 007844 | TURNER | SALESMAN  | 7698 | 1981-09-08 00:00:00 | 1500.00 |    0.00 |     30 |
|     30 | SALES      | CHICAGO  | 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 |  500.00 |     30 |
|     30 | SALES      | CHICAGO  | 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 |    NULL |     30 |
|     30 | SALES      | CHICAGO  | 007900 | JAMES  | CLERK     | 7698 | 1981-12-03 00:00:00 |  950.00 |    NULL |     30 |
|     30 | SALES      | CHICAGO  | 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 |  300.00 |     30 |
|     30 | SALES      | CHICAGO  | 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |
|     40 | OPERATIONS | BOSTON   |   NULL | NULL   | NULL      | NULL | NULL                |    NULL |    NULL |   NULL |
+--------+------------+----------+--------+--------+-----------+------+---------------------+---------+---------+--------+
15 rows in set (0.00 sec)


mysql> select  dname,ename,dept.deptno  from dept left join  emp on dept.deptno=emp.deptno  order by dept.deptno asc;
+------------+--------+--------+
| dname      | ename  | deptno |
+------------+--------+--------+
| ACCOUNTING | KING   |     10 |
| ACCOUNTING | CLARK  |     10 |
| ACCOUNTING | MILLER |     10 |
| RESEARCH   | ADAMS  |     20 |
| RESEARCH   | SCOTT  |     20 |
| RESEARCH   | FORD   |     20 |
| RESEARCH   | JONES  |     20 |
| RESEARCH   | SMITH  |     20 |
| SALES      | MARTIN |     30 |
| SALES      | ALLEN  |     30 |
| SALES      | JAMES  |     30 |
| SALES      | BLAKE  |     30 |
| SALES      | WARD   |     30 |
| SALES      | TURNER |     30 |
| OPERATIONS | NULL   |     40 |
+------------+--------+--------+
15 rows in set (0.00 sec)



http://www.kler.cn/a/383062.html

相关文章:

  • Centos Linux 7 搭建邮件服务器(postfix + dovecot)
  • #渗透测试#SRC漏洞挖掘# 操作系统-Linux系统基础02之Openssl、软连接与硬连接、用户账号数据库
  • 08LangChain实战课 - 输出解析器深入与Pydantic解析器实战
  • 【华硕天选5开机黑屏只有鼠标,调用资源管理器也无法黑屏状态的一种解决方式】
  • watch与computed的区别、运用的场景
  • HiveSQL 中判断字段是否包含某个值的方法
  • 机器学习(三)——决策树(附核心思想、重要算法、概念(信息熵、基尼指数、剪枝处理)及Python源码)
  • Flutter UI构建渲染(4)
  • Windows10/11下python脚本自动连接WiFi热点
  • STM32启动文件分析
  • Axure是什么软件?全方位解读助力设计入门
  • 实践是认识的来源
  • GPU的内存是什么?
  • 继承——面向对象编程的基石
  • 【C++】lambda表达式的理解与运用(C++11新特性)
  • [C++ 核心编程]笔记 4.4.2 类做友元
  • 【Vue 2.x】之指令详解
  • Nat Med 病理AI系列|人工智能在肝病临床试验中的应用·顶刊精析·24-11-06
  • QT开发:掌握现代UI动画技术:深入解析QML和Qt Quick中的动画效果
  • 用PyQt 5 开发的雷达基数据可视化软件
  • 关于c指针的一些说明
  • 第2篇 使用Intel FPGA Monitor Program创建基于ARM处理器的汇编或C语言工程<二>
  • 【5.10】指针算法-快慢指针将有序链表转二叉搜索树
  • Java项目实战II基于Spring Boot的问卷调查系统的设计与实现(开发文档+数据库+源码)
  • Linux 文件基本属性
  • SQL Server 日志记录