SQL的inner join、left join、right join、full outer join、union、union all
主题: SQL的inner join、left join、right join、full outer join、union、union all的学习。
Table A和Table B表如下所示:
| id | name |
| 1000 | 猫 |
| 1001 | 狗 |
| 1002 | 苹果 |
| 1003 | 香蕉 |
| id | name |
| 1004 | 猫 |
| 1005 | 狗 |
| 1006 | 栗子 |
| 1007 | 西瓜 |
1. inner join(产生TableA和TableB的交集)
SELECT * FROM TableA INNER JOIN TableB ON TableA.name = TableB.name
| 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
| 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
| 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
| name |
| 猫 |
| 狗 |
| 苹果 |
| 香蕉 |
| 栗子 |
| 西瓜 |
SELECT name FROM TableA UNION ALL SELECT name FROM TableB
| 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的更多相关文章
- EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询
场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...
- oracle 内连接(inner join)、外连接(outer join)、全连接(full join)
转自:https://premier9527.iteye.com/blog/1659689 建表语句: create table EMPLOYEE(EID NUMBER,DEPTID NUMBER,E ...
- 图解SQL的inner join、left join、right join、full outer join、union、union all的区别
SQL的Join语法有很多,inner join(等值连接) 只返回两个表中联结字段相等的行,left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录,right join(右 ...
- 图解SQL的inner join(join)、left join、right join、full outer join、union、union all的区别
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- 图解SQL的inner join、left join、right join、full outer join、union、union all的区别【转载】
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- SQL OUTER JOIN
When we want to select out all the record from two table, no matter it's present at second table or ...
- 图解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 ...
- SQL夯实基础(一):inner join、outer join和cross join的区别
一.数据构建 先建表,再说话 create database Test use Test create table A ( AID ,) primary key, name ), age int ) ...
- SQL:OUTER JOIN使用方法具体解释
SQL--JOIN使用方法 外联接. 外联接能够是左向外联接.右向外联接或完整外部联接. 在 FROM 子句中指定外联接时,能够由下列几组keyword中的一组指定: LEFT JOIN 或 LEF ...
- 图解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 是右边的表.其各有 ...
随机推荐
- 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 ...
- 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 ...
- 求n个排序链表的交集
题目大致意思是 给出n个排序list,每个list只有两个方法 (1)bool goNext(); 判断是否有下一个元素,没有元素返回false, 有元素返回true (2)int next(); 返 ...
- Leetcode Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- AngularJS的Filter用法详解
上一篇讲了自定义Directive,本篇是要讲到AngularJS的Filter. Filter简介 Filter是用来格式化数据用的. Filter的基本原型( '|' 类似于Linux中的管道模式 ...
- 通过data:image/png;base64把图片直接写在src里
从网上下了个源文件查看时候发现了引用图片的地址不是在本地上的,而是后面跟了一大串字符data:image/png;base64...查了一下资料分析如下: 关于用base64存储图片 网页上有些图片的 ...
- tshark (wireshark)笔记
1. dumpcap -i eth0 -q -n -b duration:120 -b files:5000 -s65535 -f "! ip broadcast and ! ip mult ...
- supervisor、pm2、forever坐下来聊聊
supervisor 是开发环境用.或者用nodemon,node-dev 代替了supervisor 和 nodemon,它和coffeescript兼容最好. forever 管理多个站点,每个站 ...
- Spring MVC和Struts2的比较(二)
1.Spring MVC的controller+command object模式比Struts2的Action模式更安全一些.而在Struts2中,自动数据绑定发生在Action对象上.这样,在Act ...
- signal(SIGPIPE, SIG_IGN)
文章来源:http://blog.163.com/niuxiangshan@126/blog/static/170596595201221942952676/ 当服务器close一个连接时,若cl ...