All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Sql,Server数据库定时自动备份_[SQL,Server教程]
Community,Server专题一:概述Community,Server_[Asp.Net教程]
ASP.NET,2.0打造购物车和支付系统之一_.net资料_编程技术
PHP在网站开发中常用21种功能代码_[PHP教程]
ASP.NET存取XML实例代码与注解_[Asp.Net教程]
用ASP.NET2.0如何随机读取Access记录?_.net资料_编程技术
C#中continue语句的使用方法
delphi组件VCL类库结构
ASP.NET防止SQL注入攻击常用方法_[Asp.Net教程]
关于.NET动态代理的介绍和应用简介_[Asp.Net教程]
Reading number is top 10 pictures
原子弹轰炸长崎的珍贵照片
Household design classic black and white
为什么别人说你是疯子
XuYing poker perspective garment debut
Plump, too plump!2
性感丰满身材火爆de美女2
In the world the most mysterious 21 place landscape3
So beauty, will let you spray blood4
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line1
Sora aoi calligraphy show
Download software ranking
打鸟视频
塘西风月痕
Tram sex maniac 2 (H) rar bag7
Proficient in Eclipse
Boxer's Top ten classic battle6
XML+Web+Service开发教程
Tram sex maniac 2 (H) rar bag5
Such love down(擒爱记)
虚拟机汉化软件
Eclipse 4.2.2 For Win64
归海一刀 published in(发表于) 2014/2/3 6:34:00 Edit(编辑)
left join的总结_[SQL Server教程]

left join的总结_[SQL Server教程]

left join的总结_[SQL Server教程]

EG1:通俗的讲
A left join B 的连接的记录数与A表的记录数同
A right join B 的连接的记录数与B表的记录数同
A left join B 等价B right join A
table A:
Field_K, Field_A
1 a
3 b
4 c
table B:
Field_K, Field_B
1 x
2 y
4 z
select a.Field_K, a.Field_A, b.Field_K, b.Field_B
from a left join b on a.Field_K=b.Field_K
Field_K Field_A Field_K Field_B
---------- ---------- ---------- ----------
1 a 1 x
3 b NULL NULL
4 c 4 z
select a.Field_K, a.Field_A, b.Field_K, b.Field_B
from a right join b on a.Field_K=b.Field_K
Field_K Field_A Field_K Field_B
---------- ---------- ---------- ----------
1 a 1 x
NULL NULL 2 y
4 c 4 z 这样的。
table1 table2
id,sex1 id sex2
a 1 a 4
b 0
select id,sex1,sex2 from table1 left join table2 on table1.id=table2.id
则,
id sex1 sex2
a 1 4
b 0 null
也就是说left join 则连接左边表中所有记录都会出现,如果根据连接条件在table2中找不到相关记录,则显示为null。right join 则显示右边表中的全部记录。inner join 则只有符合条件的记录才会出现在结果集中。EG2∶有两表a和b,前两字段完全相同:(id int,name varchar(10)...)
id name
----------- ----------
1 a
2 b
3 c



以下的查询语句,你知道它的运行结果吗?:
1.
select * from a left join b on a.id=b.id where a.id=1
2.
select * from a left join b on a.id=b.id and a.id=1
3.
select * from a left join b on a.id=b.id and b.id=1
4.
select * from a left join b on a.id=1



结果:
id name id name
----------- ----------- ----------- -----------
1 10 1 10


(1 row(s) affected)


id name id name
----------- ----------- ----------- -----------
1 10 1 10
2 20 NULL NULL
3 30 NULL NULL


(3 row(s) affected)


id name id name
----------- ----------- ----------- -----------
1 10 1 10
2 20 NULL NULL
3 30 NULL NULL


(3 row(s) affected)


id name id name
----------- ----------- ----------- -----------
1 10 1 10
1 10 2 20
1 10 3 30
2 20 NULL NULL
3 30 NULL NULL


(5 row(s) affected)



思路:
left join 嘛, 无非是左边表为基础, 扫描右边表匹配的记录


先是左边表的第1条记录
1 a
按条件 a.id=1, 来扫描右边表的记录
对于右边表的每条记录, 显然 a.id=1 这个条件都是成立的, 所以第1条记录匹配后的结果是:


1 a 1 a
1 a 2 b
1 a 3 c


---------------------------------------------
然后再扫描第2条记录
2 b
对于条件 a.id=1, 在边表中没有与之匹配的记录, 所以右边表为NULL
因此第2条记录匹配的结果为
2 b NULL NULL


----------------------------------------------
第3条记录与第2条记录一样, 匹配的结果是
3 c NULL NULL


---------------------------------------
因此最终结果是5条记录
1 a 1 a
1 a 2 b
1 a 3 c
2 b null null
3 c null null



内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符)。包括相等联接和自然联接。
内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行。例如,检索 students 和 courses 表中学生标识号相同的所有行。


外联接,外联接可以是左向外联接、右向外联接或完整外部联接。
在 FROM 子句中指定外联接时,可以由下列几组关键字中的一组指定:


LEFT JOIN 或 LEFT OUTER JOIN。
左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。


RIGHT JOIN 或 RIGHT OUTER JOIN。
右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。


FULL JOIN 或 FULL OUTER JOIN。
完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。


交叉联接。
交叉联接返回左表中的所有行,左表中的每一行与右表中的所有行组合。交叉联接也称作笛卡尔积。




添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.