SQL内连接与外连接的区别

要这个问题的 答案,没有用的就不要发了,这个问题是准备考试的
请高手帮忙解答一下!
谢谢

SQL内连接与外连接的共有3点不同:

1、两者的分类不同:内连接分为相等连接和自然连接两种连接方式;而外连接分为左外连接、右外连接和全外连接三种连接方式(左外连接即LEFT OUTER JOIN;右外连接即RIGHT OUTER JOIN)。

2、两者所连接的对象表不同:内连接进行连接的两个表是对应的相匹配的字段完全相同的。左外连接中进行连接的两个表会返回左边表中的所有的行和右边表中与之相匹配的列值,没有相匹配的用空值代替。右外连接中进行连接的两个表会返回右边表中的所有的行和左边表中与之相匹配的列值,没有相匹配的用空值代替。

3、两者的作用范围不同:内连接的连接发生在一张基表内,而外连接的连接发生在两张表之间。

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

温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-12-31
内联接:显示两个表想匹配的行,左连接显示JION左表的行,右表没有想匹配的,用NUL代替!右联接和左连接相反,全连接和左右连接的合计。

使用左向外联接
假设在 city 列上联接 authors 表和 publishers 表。结果只显示在出版商所在城市居住的作者(本例中为 Abraham Bennet 和 Cheryl Carson)。

若要在结果中包括所有的作者,而不管出版商是否住在同一个城市,请使用 SQL-92 左向外联接。下面是 Transact-SQL 左向外联接的查询和结果:

USE pubs
SELECT a.au_fname, a.au_lname, p.pub_name
FROM authors a LEFT OUTER JOIN publishers p
ON a.city = p.city
ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC

下面是结果集:

au_fname au_lname pub_name
-------------------- ------------------------------ -----------------
Reginald Blotchet-Halls NULL
Michel DeFrance NULL
Innes del Castillo NULL
Ann Dull NULL
Marjorie Green NULL
Morningstar Greene NULL
Burt Gringlesby NULL
Sheryl Hunter NULL
Livia Karsen NULL
Charlene Locksley NULL
Stearns MacFeather NULL
Heather McBadden NULL
Michael O'Leary NULL
Sylvia Panteley NULL
Albert Ringer NULL
Anne Ringer NULL
Meander Smith NULL
Dean Straight NULL
Dirk Stringer NULL
Johnson White NULL
Akiko Yokomoto NULL
Abraham Bennet Algodata Infosystems
Cheryl Carson Algodata Infosystems

(23 row(s) affected)

不管是否与 publishers 表中的 city 列匹配,LEFT OUTER JOIN 均会在结果中包含 authors 表的所有行。注意:结果中所列的大多数作者都没有相匹配的数据,因此,这些行的 pub_name 列包含空值。

使用右向外联接
假设在 city 列上联接 authors 表和 publishers 表。结果只显示在出版商所在城市居住的作者(本例中为 Abraham Bennet 和 Cheryl Carson)。SQL-92 右向外联接运算符 RIGHT OUTER JOIN 指明:不管第一个表中是否有匹配的数据,结果将包含第二个表中的所有行。

若要在结果中包括所有的出版商,而不管城市中是否还有出版商居住,请使用 SQL-92 右向外联接。下面是 Transact-SQL 右向外联接的查询和结果:

USE pubs
SELECT a.au_fname, a.au_lname, p.pub_name
FROM authors AS a RIGHT OUTER JOIN publishers AS p
ON a.city = p.city
ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC

下面是结果集:

au_fname au_lname pub_name
-------------------- ------------------------ --------------------
Abraham Bennet Algodata Infosystems
Cheryl Carson Algodata Infosystems
NULL NULL Binnet & Hardley
NULL NULL Five Lakes Publishing
NULL NULL GGG&G
NULL NULL Lucerne Publishing
NULL NULL New Moon Books
NULL NULL Ramona Publishers
NULL NULL Scootney Books

(9 row(s) affected)

使用谓词(如将联接与常量比较)可以进一步限制外联接。下例包含相同的右向外联接,但消除销售量低于 50 本的书籍的书名:

USE pubs
SELECT s.stor_id, s.qty, t.title
FROM sales s RIGHT OUTER JOIN titles t
ON s.title_id = t.title_id
AND s.qty > 50
ORDER BY s.stor_id ASC

下面是结果集:

stor_id qty title
------- ------ ---------------------------------------------------------
(null) (null) But Is It User Friendly?
(null) (null) Computer Phobic AND Non-Phobic Individuals: Behavior
Variations
(null) (null) Cooking with Computers: Surreptitious Balance Sheets
(null) (null) Emotional Security: A New Algorithm
(null) (null) Fifty Years in Buckingham Palace Kitchens
7066 75 Is Anger the Enemy?
(null) (null) Life Without Fear
(null) (null) Net Etiquette
(null) (null) Onions, Leeks, and Garlic: Cooking Secrets of the
Mediterranean
(null) (null) Prolonged Data Deprivation: Four Case Studies
(null) (null) Secrets of Silicon Valley
(null) (null) Silicon Valley Gastronomic Treats
(null) (null) Straight Talk About Computers
(null) (null) Sushi, Anyone?
(null) (null) The Busy Executive's Database Guide
(null) (null) The Gourmet Microwave
(null) (null) The Psychology of Computer Cooking
(null) (null) You Can Combat Computer Stress!
第2个回答  推荐于2017-09-01
你是要弄清楚区别在什么地方还是单纯想要文字说明
文字说明的楼上说了一大堆了,不说了。
弄个例题,直观一点。两个表:
--表stu
id name
1, Jack
2, Tom
3, Kity
4, nono
--表exam
id grade
1, 56
2, 76
11, 89

内连接 (显示两表id匹配的)
select stu.id,exam.id,stu.name, exam.grade from stu inner join exam on stu.id=exam.id
stu.id exam.id name grade
--------------------------------
1 1 Jack 56
2 2 Tom 76

左连接(显示join 左边的表的所有数据,exam只有两条记录,所以stu.id,grade 都用NULL 显示)
select stu.id,exam.id,stu.name, exam.grade from stu left join exam on stu.id=exam.id
1 1 Jack 56
2 2 Tom 76
3 NULL Kity NULL
4 NULL nono NULL

右连接(与作连接相反,显示join右边表的所有数据)
select stu.id,exam.id,stu.name, exam.grade from stu right join exam on stu.id=exam.id
1 1 Jack 56
2 2 Tom 76
NULL 11 NULL 89本回答被提问者采纳
第3个回答  2008-12-31
内连接:进行连接的两个表对应的相匹配的字段完全相同的连接。

外连接又分为左外连接和右外连接。
左连接即LEFT OUTER JOIN:
两个表进行左连接时会返回左边表中的所有的行和右边表中与之相匹配的列值没有相匹配的用空值代替。

右连接即RIGHT OUTER JOIN:
两个表进行右连接时会返回右边表中的所有的行和左边表中与之相匹配的列值没有相匹配的用空值代替。

能看明白吗。。。
第4个回答  2008-12-30
内连接取交集,外连接分左和右,
左连接左边的全取,
右连接右边的全取
相似回答