Right Outer Join 運算子返回滿足第二個(底端)輸入與第一個(頂端)輸入的每個匹配行的聯結的每行。此外,它還返回第二個輸入中在第一個輸入中沒有匹配行的任何行,即與 NULL 聯結。如果 Argument 列內不存在任何聯結謂詞,則每行都是一個匹配行。
一、聯結( join)
通過聯結,可以根據各個表之間的邏輯關係從兩個或多個表中檢索資料。聯結表示應如何使用一個表中的資料來選擇另一個表中的行。
聯結條件通過以下方法定義兩個表在查詢中的關聯方式:
指定每個表中要用於聯結的列。典型的聯結條件在一個表中指定外鍵,在另一個表中指定與其關聯的鍵。指定比較各列的值時要使用的邏輯運算子(=、<> 等)。
下面的 SQL 陳述式說明 titles 表和 publishers 表之間的左向外聯結包括所有的書名,甚至包括那些沒有出版商資訊的書名: Use pubs SELECT titles.title_id, titles.title, publishers.pub_name FROM titles LEFT OUTER JOIN publishers ON titles.pub_id = publishers.pub_id
執行個體
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int )
5> GO
1>
2> create table job(
3> ID int,
4> title nvarchar (10),
5> averageSalary int)
6> GO
1>
2>
3> insert into employee (ID, name, salary) values (1, 'Jason', 1234)
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (2, 'Robert', 4321)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (3, 'Celia', 5432)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (4, 'Linda', 3456)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (5, 'David', 7654)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (6, 'James', 4567)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (7, 'Alison', 8744)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (8, 'Chris', 9875)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (9, 'Mary', 2345)
2> GO
(1 rows affected)
1>
2> insert into job(ID, title, averageSalary) values(1,'Developer',3000)
3> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(2,'Tester', 4000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(3,'Designer', 5000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(4,'Programmer', 6000)
2> GO
(1 rows affected)
1>
2>
3>
4> select * from employee;
5> GO
ID name salary
----------- ---------- -----------
1 Jason 1234
2 Robert 4321
3 Celia 5432
4 Linda 3456
5 David 7654
6 James 4567
7 Alison 8744
8 Chris 9875
9 Mary 2345
(9 rows affected)
1> select * from job;
2> GO
ID title averageSalary
----------- ---------- -------------
1 Developer 3000
2 Tester 4000
3 Designer 5000
4 Programmer 6000
(4 rows affected)
1>
2> -- RIGHT OUTER JOIN includes all the information from the table on the right.
3>
4> SELECT e.id, e.name, j.title
5> FROM Employee e
6> RIGHT OUTER JOIN job j
7> ON e.id = j.id
8> GO
id name title
----------- ---------- ----------
1 Jason Developer
2 Robert Tester
3 Celia Designer
4 Linda Programmer
(4 rows affected)
1>
2>
3> drop table employee;
4> drop table job;
5> GO
1>
2>
執行個體二 RIGHT OUTER JOIN
外串連(Outer join)文法
左外串連的文法為: SELECT 列 FROM 表1 LEFT [OUTER]JOIN 表2 ON 表1.列1=表2.列2
右外串連的文法為: SELECT select_list FROM 表1 RIGHT[OUTER]JOIN 表2 ON 表1.列1=表2.列2
全外串連(完整外部聯結)的文法為: SELECT select_list FROM 表1 FULL[OUTER] JOIN 表2 ON 表1.列1=表2.列2
執行個體
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int )
5> GO
1>
2> create table job(
3> ID int,
4> title nvarchar (10),
5> averageSalary int)
6> GO
1>
2>
3> insert into employee (ID, name, salary) values (1, 'Jason', 1234)
4> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (2, 'Robert', 4321)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (3, 'Celia', 5432)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (4, 'Linda', 3456)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (5, 'David', 7654)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (6, 'James', 4567)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (7, 'Alison', 8744)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (8, 'Chris', 9875)
2> GO
(1 rows affected)
1> insert into employee (ID, name, salary) values (9, 'Mary', 2345)
2> GO
(1 rows affected)
1>
2> insert into job(ID, title, averageSalary) values(1,'Developer',3000)
3> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(2,'Tester', 4000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(3,'Designer', 5000)
2> GO
(1 rows affected)
1> insert into job(ID, title, averageSalary) values(4,'Programmer', 6000)
2> GO
(1 rows affected)
1>
2>
3> select * from employee;
4> GO
ID name salary
----------- ---------- -----------
1 Jason 1234
2 Robert 4321
3 Celia 5432
4 Linda 3456
5 David 7654
6 James 4567
7 Alison 8744
8 Chris 9875
9 Mary 2345
(9 rows affected)
1> select * from job;
2> GO
ID title averageSalary www.111cn.net/6700s/
----------- ---------- -------------
1 Developer 3000
2 Tester 4000
3 Designer 5000
4 Programmer 6000
(4 rows affected)
1>
2>
3> SELECT e.name FROM Employee e
4> RIGHT OUTER JOIN job j
5> ON e.id = j.id
6> WHERE e.id IS NOT NULL
7> GO
name
----------
Jason
Robert
Celia
Linda
(4 rows affected)
1>
2>
3> drop table employee;
4> drop table job;
5> GO
1>