【注意】:Oracle数据库支持full join,mysql是不支持full join的,但仍然可以同过左外连接+ union+右外连接实现

初始化SQL语句:

  1.  
    /*join 建表语句*/
  2.  
    drop database if exists test;
  3.  
    create database test;
  4.  
    use test;
  5.  
     
  6.  
    /* 左表t1*/
  7.  
    drop table if exists t1;
  8.  
    create table t1 (id int not null,name varchar(20));
  9.  
    insert into t1 values (1,'t1a');
  10.  
    insert into t1 values (2,'t1b');
  11.  
    insert into t1 values (3,'t1c');
  12.  
    insert into t1 values (4,'t1d');
  13.  
    insert into t1 values (5,'t1f');
  14.  
     
  15.  
    /* 右表 t2*/
  16.  
    drop table if exists t1;
  17.  
    create table t2 (id int not null,name varchar(20));
  18.  
    insert into t2 values (2,'t2b');
  19.  
    insert into t2 values (3,'t2c');
  20.  
    insert into t2 values (4,'t2d');
  21.  
    insert into t2 values (5,'t2f');
  22.  
    insert into t2 values (6,'t2a');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

1、笛卡尔积

两表关联,把左表的列和右表的列通过笛卡尔积的形式表达出来。

mysql> select * from t1 join t2;
  • 1

2、左连接

两表关联,左表全部保留,右表关联不上用null表示。

mysql> select * from t1 left join t2 on t1.id = t2.id;
  • 1

3、右连接

右表全部保留,左表关联不上的用null表示。

mysql> select * from t1 right join t2 on t1.id =t2.id;
  • 1

4、内连接

两表关联,保留两表中交集的记录。

mysql> select * from t1 inner join t2 on t1.id = t2.id;
  • 1

5、左表独有

两表关联,查询左表独有的数据。

mysql> select * from t1 left join t2 on t1.id = t2.id where t2.id is null;
  • 1

6、右表独有

两表关联,查询右表独有的数据。

mysql> select * from t1 right join t2 on t1.id = t2.id where t1.id is  null;
  • 1

7、全连接

两表关联,查询它们的所有记录。

oracle里面有full join,但是在mysql中没有full join。我们可以使用union来达到目的。

  1.  
    mysql> select * from t1 left join t2 on t1.id = t2.id
  2.  
    -> union
  3.  
    -> select * from t1 right join t2 on t1.id = t2.id;
  • 1
  • 2
  • 3

8、并集去交集

两表关联,取并集然后去交集。

  1.  
    mysql> select * from t1 left join t2 on t1.id = t2.id where t2.id is null
  2.  
    -> union
  3.  
    -> select * from t1 right join t2 on t1.id = t2.id where t1.id is null;
  • 1
  • 2
  • 3

mysql中的几种join 及 full join问题的更多相关文章

  1. MySQL中的两种临时表

    MySQL中的两种临时表 伯乐在线2016-07-06 05:16:52阅读(4556)评论(3) 声明:本文由入驻搜狐公众平台的作者撰写,除搜狐官方账号外,观点仅代表作者本人,不代表搜狐立场.举报 ...

  2. MySQL 中的两种临时表

    来源:阿里云RDS - 数据库内核组 链接:http://mysql.taobao.org/monthly/2016/06/07/ 外部临时表 通过CREATE TEMPORARY TABLE 创建的 ...

  3. 浅谈SQL Server中的三种物理连接操作(HASH JOIN MERGE JOIN NESTED LOOP)

    简介 在SQL Server中,我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge J ...

  4. mysql中的几种日志了解

    前言 MySQL中有以下日志文件,分别是: 1:重做日志(redo log) 2:回滚日志(undo log) 3:二进制日志(binlog) 4:错误日志(errorlog) 5:慢查询日志(slo ...

  5. mysql中的四种常用的引擎

    MySQL常用的引擎有:InnoDB存储引擎.MyISAM存储引擎.MEMORY存储引擎.Archive存储引擎 InnoDB存储引擎 InnoDB是事务型数据库的首选引擎,支持事务安全表(ACID) ...

  6. 关于MySQL中的三种日期类型

    Mysql中我们经常用来存储日期的数据类型有三种:Date.Datetime.Timestamp. Date数据类型用来存储没有时间的日期.Mysql获取和显示这个类型的格式为"YYYY-M ...

  7. 【mysql】mysql中单列索引、联合索引、Join联表查询建立索引 和 EXPLAIN的分析使用

    2.创建联合索引,从坐到右分别为:userid.openId.name   2. #### --------------  多表联合查询 update 2019/03/13  ------------ ...

  8. mysql中select五种子句和统计函数

    select 五种子句顺序 where 条件 group by 分组 having 把结果进行再次筛选 order by  排序 limit  取出条目 统计函数  max(列名)  求最大 min( ...

  9. MySQL中四种隔离级别的演示

    事务的隔离是并发操作中需要理解清楚的问题.MySQL中共有4种不同的隔离级别,这4种隔离级别分别是: 隔离级别类型 影响结果 READ UNCOMMITTED(未提交读) 事务将会读取到未提交的数据, ...

随机推荐

  1. STM32 --- 什么时候打开复用IO的时钟(比如RCC_APB2Periph_AFIO)

    需要用到外设的重映射功能时才需要使能AFIO的时钟,包括外部中断. 外部中断(EXTI)中与AFIO有关的寄存器是AFIO-EXTICR1.2.3,它们是用来选择EXTIx外部中断的输入脚之用. 举例 ...

  2. python爬虫-采集英语翻译

      http://fanyi.baidu.com/?aldtype=85#en/zh/drughttp://fanyi.baidu.com/?aldtype=85#en/zh/cathttp://fa ...

  3. GO语言的进阶之路-go的程序结构以及包简介

    GO语言的进阶之路-go的程序结构以及包简介 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编辑,编译和运行 A,编辑 Go程序使用UTF-8编码的纯Unicode文本编写.大 ...

  4. tomcat配置好后,启动eclipse中的server,不能出现有猫的页面,提示404

    原因:tomcat与eclipse中的server未关联起来 解决办法:双击servers中的server,在Server Locations中选中第二项,保存之后再进行刚才的操作就好了.

  5. SQL记录-rowid去重

    select * from a where rowid=(select max(rowid) from a)

  6. 在IIS上启用Gzip压缩(HTTP压缩)

    一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 二.前言. 本文的知识点是从互联网收集整理, 主要来源于中文wiki.  使用Y ...

  7. JavaScript基本操作之——九个 Console 命令

    一.显示信息的命令 console.log('hello'); console.info('信息'); console.error('错误'); console.warn('警告'); 二.占位符 c ...

  8. ELF格式探析之三:sections

    前文链接: ELF格式探析之一:Segment和Section ELF格式探析之二:文件头ELF Header详解 今天我们讲对目标文件(可重定位文件)和可执行文件都很重要的section. 我们在讲 ...

  9. 20155215 2016-2017-2 《Java程序设计》第9周学习总结

    20155215 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 JDBC入门 - JDBC(Java DataBase Connectivity) ...

  10. android SQLiteOpenHelper 使用

    1.实体 package mydemo.mycom.demo2.entity; public class UserInfo { private int id; private String usern ...