主题: SQL的inner join、left join、right join、full outer join、union、union all的学习。


Table A和Table B表如下所示:

表A
id name
1000
1001
1002 苹果
1003 香蕉
表B
id name
1004
1005
1006 栗子
1007 西瓜

1. inner join(产生TableA和TableB的交集)

SELECT * FROM TableA INNER JOIN TableB ON TableA.name = TableB.name
TableA inner join TableB
id name id name
1000 1004
1001 1005

2. full [outter] join(产生TableA和TableB的并集)

SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.name 
TableA full outter join TableB
id name id name
1000 1004
1001 1005
1002 苹果 null null
1003 香蕉 null null
null null 1006 栗子
null null 1007 西瓜

对于没有匹配的记录,则会以null做为值。

3. left [outter] join(产生表A的完全集,而B表中匹配的则有值,没有匹配的则以null值取代)

SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.name = TableB.name
TableA left outter join TableB
id name id name
1000 1004
1001 1005
1002 苹果 null null
1003 香蕉 null null

right [outer] join, 是以后面的表为基础,与left [outter] join 类似。

4. union 、 union all(UNION 操作符用于合并两个或多个 SELECT 语句的结果集)

SELECT name FROM TableA UNION SELECT name FROM TableB
union
name
苹果
香蕉
栗子
西瓜
SELECT name FROM TableA UNION ALL SELECT name FROM TableB
union all
name
苹果
香蕉
栗子
西瓜
SELECT * FROM TableA UNION SELECT * FROM TableB
 
id name
1000
1001
1002 苹果
1003 香蕉
1004
1005
1006 栗子
1007 西瓜

SQL的inner join、left join、right join、full outer join、union、union all的更多相关文章

  1. EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询

    场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...

  2. oracle 内连接(inner join)、外连接(outer join)、全连接(full join)

    转自:https://premier9527.iteye.com/blog/1659689 建表语句: create table EMPLOYEE(EID NUMBER,DEPTID NUMBER,E ...

  3. 图解SQL的inner join、left join、right join、full outer join、union、union all的区别

    SQL的Join语法有很多,inner join(等值连接) 只返回两个表中联结字段相等的行,left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录,right join(右 ...

  4. 图解SQL的inner join(join)、left join、right join、full outer join、union、union all的区别

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  5. 图解SQL的inner join、left join、right join、full outer join、union、union all的区别【转载】

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  6. SQL OUTER JOIN

    When we want to select out all the record from two table, no matter it's present at second table or ...

  7. 图解SQL inner join、left join、right join、full outer join、union、union all的区别

    转于:http://justcoding.iteye.com/blog/2006487 这是一篇来自Coding Horror的文章. SQL的Join语法有很多:有inner的,有outer的,有l ...

  8. SQL夯实基础(一):inner join、outer join和cross join的区别

    一.数据构建 先建表,再说话 create database Test use Test create table A ( AID ,) primary key, name ), age int ) ...

  9. SQL:OUTER JOIN使用方法具体解释

    SQL--JOIN使用方法 外联接. 外联接能够是左向外联接.右向外联接或完整外部联接.  在 FROM 子句中指定外联接时,能够由下列几组keyword中的一组指定: LEFT JOIN 或 LEF ...

  10. 图解SQL的inner join、left join、right join、full outer join、union、union all的区别

    转自:http://blog.csdn.net/jz20110918/article/details/41806611 假设我们有两张表.Table A 是左边的表.Table B 是右边的表.其各有 ...

随机推荐

  1. CF# Educational Codeforces Round 3 B. The Best Gift

    B. The Best Gift time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547

    Sudoku Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  3. 求n个排序链表的交集

    题目大致意思是 给出n个排序list,每个list只有两个方法 (1)bool goNext(); 判断是否有下一个元素,没有元素返回false, 有元素返回true (2)int next(); 返 ...

  4. Leetcode Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. AngularJS的Filter用法详解

    上一篇讲了自定义Directive,本篇是要讲到AngularJS的Filter. Filter简介 Filter是用来格式化数据用的. Filter的基本原型( '|' 类似于Linux中的管道模式 ...

  6. 通过data:image/png;base64把图片直接写在src里

    从网上下了个源文件查看时候发现了引用图片的地址不是在本地上的,而是后面跟了一大串字符data:image/png;base64...查了一下资料分析如下: 关于用base64存储图片 网页上有些图片的 ...

  7. tshark (wireshark)笔记

    1. dumpcap -i eth0 -q -n -b duration:120 -b files:5000 -s65535 -f "! ip broadcast and ! ip mult ...

  8. supervisor、pm2、forever坐下来聊聊

    supervisor 是开发环境用.或者用nodemon,node-dev 代替了supervisor 和 nodemon,它和coffeescript兼容最好. forever 管理多个站点,每个站 ...

  9. Spring MVC和Struts2的比较(二)

    1.Spring MVC的controller+command object模式比Struts2的Action模式更安全一些.而在Struts2中,自动数据绑定发生在Action对象上.这样,在Act ...

  10. signal(SIGPIPE, SIG_IGN)

    文章来源:http://blog.163.com/niuxiangshan@126/blog/static/170596595201221942952676/   当服务器close一个连接时,若cl ...