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 是右边的表.其各有 ...
随机推荐
- JSP 中 pageEncoding 和 charset 区别以及中文乱码解决方案
一.JSP 中 pageEndcodeing 和 charset 的作用 <%@ page contentType="text/html;charset=GB2312"%&g ...
- AngularJS内置指令
指令,我将其理解为AngularJS操作HTML element的一种途径. 由于学习AngularJS的第一步就是写内置指令ng-app以指出该节点是应用的根节点,所以指令早已不陌生. 这篇日志简单 ...
- Android Immersive Mode (沉浸模式) 还是 Translucent Bars (透明状态栏)
Immersive Mode (沉浸模式) 还是 Translucent Bars (透明状态栏) [科普]什么叫真正的“沉浸式”状态栏? 为什么在国内会有很多用户把「透明栏」(Translucent ...
- Leetcode Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 《深入浅出Windows 10通用应用开发》
<深入浅出Windows 10通用应用开发>采用Windows 10的SDK进行重新改版,整合了<深入浅出Windows Phone 8.1应用开发>和<深入解析 ...
- UVA 10780 - Again Prime? No Time.
题目链接 思路好想,注意细节.错了很多次. #include <cstdio> #include <cstring> #include <string> #incl ...
- Java中UIManager的几种外观的详细讲解
Java'中的几种Look and Feel 1.Metal风格 (默认) String lookAndFeel = "javax.swing.plaf.metal.MetalLookAnd ...
- C语言中if语句
C语言if语句后面的表达式 C语言中if关键字之后(即括号内)均为表达式. 该表达式通常是逻辑表达式或关系表达式,但也可以是其它表达式,如赋值表达式等,甚至也可以是一个变量,这些变量的值都换算成了逻辑 ...
- [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字
Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...
- Hadoop.2.x_时间服务器搭建(CentOs6.6)
一.检查linux系统NTP是否被安装 [liuwl@hadoop09-linux-01 ~]$ [liuwl@hadoop09-linux-01 ~]$ rpm -qa | grep ntp ntp ...