用实例展示left Join,right join,inner join,join,cross join,union 的区别
1.向TI,T2插入数据:
T1 7条
ID Field2 Field3 Field4
1 1 3 54
2 1 3 54
3 1 3 54
4 2 3 54
5 3 3 54
6 4 3 54
7 5 3 54
T2 7条
IDField22Field23
1133
2133
3133
4133
5233
6333
7 9 33
2.测试left join
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 left join dbo.T2 on t1.Field2=t2.Field22
row_numIDField2Field3Field4IDField22Field23
1113541133
2113542133
3113543133
4113544133
5213541133
6213542133
7213543133
8213544133
9313541133
10313542133
11313543133
12313544133
13423545233
14533546333
NULLNULLNULL
NULLNULLNULL
3.测试right join
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 right join dbo.T2 on t1.Field2=t2.Field22
row_numIDField2Field3Field4IDField22Field23
2113541133
3113542133
4113543133
5113544133
6213541133
7213542133
8213543133
9213544133
10313541133
11313542133
12313543133
13313544133
14423545233
15533546333
4.测试join
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 join dbo.T2 on t1.Field2=t2.Field22
row_numIDField2Field3Field4IDField22Field23
1113541133
2113542133
3113543133
4113544133
5213541133
6213542133
7213543133
8213544133
9313541133
10313542133
11313543133
12313544133
13423545233
14533546333
5.测试inner join
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 inner join dbo.T2 on t1.Field2=t2.Field22
row_numIDField2Field3Field4IDField22Field23
1113541133
2113542133
3113543133
4113544133
5213541133
6213542133
7213543133
8213544133
9313541133
10313542133
11313543133
12313544133
13423545233
14533546333
6.测试cross join
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 cross join dbo.T2 on t1.Field2=t2.Field22
消息156,级别15,状态1,第1 行
关键字'on' 附近有语法错误。
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 cross join dbo.T2
row_numIDField2Field3Field4IDField22Field23
1113541133
2113542133
3113543133
4113544133
5113545233
6113546333
7113548933
8213541133
9213542133
10213543133
11213544133
12213545233
13213546333
14213548933
15313541133
16313542133
17313543133
18313544133
19313545233
20313546333
21313548933
22423541133
23423542133
24423543133
25423544133
26423545233
27423546333
28423548933
29533541133
30533542133
31533543133
32533544133
33533545233
34533546333
35533548933
36643541133
37643542133
38643543133
39643544133
40643545233
41643546333
42643548933
43753541133
44753542133
45753543133
46753544133
47753545233
48753546333
49753548933
7.测试unoin
select * from dbo.T1 union all select * from dbo.T2
消息205,级别16,状态1,第1 行
使用UNION、INTERSECT 或EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。
select Field2 from dbo.T1 union all select Field22 from dbo.T2
Field2
1
1
1
2
3
4
5
1
1
1
1
2
3
9
with T3 as
( select Field2 from dbo.T1 union all select Field22 from dbo.T2 )
select ROW_NUMBER() over (order by T3.field2) as row_num,* from T3
row_numField2
11
21
31
41
51
61
71
82
92
103
113
124
135
149
8.生成数据库的脚本
USE [Demo]
GO
/****** Object: Table [dbo].[T1] Script Date: 09/27/2013 23:19:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[T1](
) NOT NULL,
[Field2] [int] NULL,
[Field3] [int] NULL,
[Field4] [int] NULL,
CONSTRAINT [PK_T1] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
USE [Demo]
GO
/****** Object: Table [dbo].[T2] Script Date: 09/27/2013 23:19:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[T2](
) NOT NULL,
[Field22] [int] NOT NULL,
[Field23] [int] NOT NULL,
CONSTRAINT [PK_T2] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
delete from dbo.T1
truncate table dbo.T2
select * from T1
select * from T2
)
)
)
)
)
)
)
truncate table dbo.T2
)
)
)
)
)
)
)
)
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 left join dbo.T2 on t1.Field2=t2.Field22
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 right join dbo.T2 on t1.Field2=t2.Field22
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 join dbo.T2 on t1.Field2=t2.Field22
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 inner join dbo.T2 on t1.Field2=t2.Field22
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 cross join dbo.T2 on t1.Field2=t2.Field22
select ROW_NUMBER()over (order by t1.id,t2.id) as row_num,* from dbo.T1 cross join dbo.T2
select * from dbo.T1 union all select * from dbo.T2
select Field2 from dbo.T1 union all select Field22 from dbo.T2
with T3 as
( select Field2 from dbo.T1 union all select Field22 from dbo.T2 )
select ROW_NUMBER() over (order by T3.field2) as row_num,* from T3
用实例展示left Join,right join,inner join,join,cross join,union 的区别的更多相关文章
- T-SQL 中的CROSS JOIN用法(半翻译)
突然发现个很吊的链接,我们来看看学习数据库要做些什么,胆小慎点:DBA工作内容!!!! 今天来翻译一篇关于T-SQL的文章,本文可供微软认证70-461:QueryingMicrosoft SQL S ...
- 高级T-SQL进阶系列 (一)【下篇】:使用 CROSS JOIN 介绍高级T-SQL
[译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 性能考虑产生了笛卡尔积的这个CROSS JOIN操作符具有一些性能方面的问题需要考虑.因为SQL引擎需要将一个数 ...
- SQL CROSS JOIN
最近在讲到T-SQL查询的Join部分时,一下子没有想起来CROSS JOIN的用法,因为其实平常也确实基本不用到.特意找了一个例子,以供参考 CROSS JOIN又称为笛卡尔乘积,实际上是把两个表乘 ...
- CROSS JOIN
原文:CROSS JOIN 最近在讲到T-SQL查询的Join部分时,一下子没有想起来CROSS JOIN的用法,因为其实平常也确实基本不用到.特意找了一个例子,以供参考 CROSS JOIN又称为笛 ...
- MySQL应用之CROSS JOIN用法简介教程
目录 2. cross join用法 @ 本博客翻译自两篇博客的: http://www.mysqltutorial.org/mysql-cross-join/ https://www.w3resou ...
- 通俗易懂的join、left join、right join、full join、cross join
内连接:列出与连接条件匹配的数据行(join\inner join) 外连接:两表合并,如有不相同的列,另外一个表显示null(left join\right join\full outer join ...
- 内连接、左外连接、右外连接、全外连接、交叉连接(CROSS JOIN)-----小知识解决大数据攻略
早就听说了内连接与外连接,以前视图中使用过.这次自考也学习了,只是简单理解,现在深入探究学习(由于上篇博客的出现)与实践: 概念 关键字: 左右连接 数据表的连接有: 1.内连接(自然连接): 只有两 ...
- 【数据库】left join(左关联)、right join(右关联)、inner join(自关联)的区别
left join(左关联).right join(右关联).inner join(自关联)的区别 用一张图说明三者的区别: 总结: left join(左联接) 返回包括左表中的所有记录和右表中关联 ...
- Python之路(第四十二篇)线程相关的其他方法、join()、Thread类的start()和run()方法的区别、守护线程
一.线程相关的其他方法 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getName(): 返回线程名. # setName(): 设置线程名. threadin ...
随机推荐
- Android基于基于布局嵌套的页面导航实现
页面如下: 主页面的布局分隔为三部分: 注意观察上面标记为红色的android:id均采用android系统默认的名称: 页面的导航组件: <?xml version="1.0&quo ...
- Mysql 多表联合查询效率分析及优化
1. 多表连接类型 1. 笛卡尔积(交叉连接) 在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用',' 如: SELECT * FROM table1 CROSS JO ...
- 不知道帐号密码的情况下完全重装Mac Min的OS X10.7系统
现状: 1.原系统OS X 10.7 2.老账号不知道密码 3.Mac小盒子 目的: 1.删除老账号 2.更新系统到10.9以上 尝试过程1: 1.按住option键 + 开机 2.选择“磁盘工具” ...
- 从windows转向mac
键盘问题: 按键对应表 Windows Mac ctrl command alt option 由此可推断,windows下的ctrl+c/v 变成了mac下的 command+c/v 功能对应表 删 ...
- 安装centos 7 体验安装过程
1. 下载centos7的iso文件,放入虚拟机的启动光盘中 2. 选择启动,然后可以看到安装的选项 3. 进行安装之后,会自动的进行一个检查 4. 选择安装的语言 5. 进入之后,可以看到所有需要安 ...
- 在这个年纪,打DOTA冲分有那么可笑么?
大学四年,毕业两年,6年dota,总是在最无助的时候让心灵得到一丝安宁的东西,烟和dota. 我不知道dota对别人的意义是什么,一盘dota,在最多不超过1个半小时的时间里,仿佛经历了一个小人生,每 ...
- Aggregation(1):Blending、Bagging、Random Forest
假设我们有很多机器学习算法(可以是前面学过的任何一个),我们能不能同时使用它们来提高算法的性能?也即:三个臭皮匠赛过诸葛亮. 有这么几种aggregation的方式: 一些性能不太好的机器学习算法(弱 ...
- Hadoop-安装过程-单虚拟机版(伪分布式)(Ubuntu13.04版本下安装)
由于新装的Ubutu默认情况下,系统只安装了SSH客户端,需要自行安装SSH服务端 如何确定是否安装了SSH服务端? 可以通过命令ssh localhost,结果如下,即未安装SSH服务端: 安装 ...
- SDN环境搭建(mininet,OVS,ryu安装及命令)
1.mininet安装与使用 1.1mininet安装 ubuntu 12.04/14.04/14.10 命令行 sudo apt-get install mininet 1.2 mini ...
- openstack 云平台API
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAKrCAIAAACV8EEMAAAgAElEQVR4nOydeVgUaZ7n/W9299nd7n