惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
O
OpenAI News
Y
Y Combinator Blog
MyScale Blog
MyScale Blog
C
Check Point Blog
Vercel News
Vercel News
小众软件
小众软件
The Register - Security
The Register - Security
N
News and Events Feed by Topic
腾讯CDC
S
SegmentFault 最新的问题
H
Heimdal Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
Secure Thoughts
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Schneier on Security
Schneier on Security
G
GRAHAM CLULEY
云风的 BLOG
云风的 BLOG
S
Schneier on Security
J
Java Code Geeks
L
LINUX DO - 最新话题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 热门话题
Scott Helme
Scott Helme
爱范儿
爱范儿
GbyAI
GbyAI
Simon Willison's Weblog
Simon Willison's Weblog
L
Lohrmann on Cybersecurity
Cloudbric
Cloudbric
W
WeLiveSecurity
The Hacker News
The Hacker News
V
V2EX
Last Week in AI
Last Week in AI
Hacker News: Ask HN
Hacker News: Ask HN
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
Cyberwarzone
Cyberwarzone
Google Online Security Blog
Google Online Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Security @ Cisco Blogs
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security
U
Unit 42
Webroot Blog
Webroot Blog
Martin Fowler
Martin Fowler
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed

博客园 - EWang

如何在MS Project中进行挣值分析 - EWang - 博客园 API函数大全 - EWang - 博客园 Bugzilla, CVS, ClearQuest, ClearCase安装心得(转) - EWang SN大全 - EWang - 博客园 Java中iText使用技巧 - EWang - 博客园 Linux下时间的修改 - EWang - 博客园 Asp.net调试的一些问题 - EWang - 博客园 SQL字符串处理函数大全 - EWang - 博客园 crontab 格式 - EWang - 博客园 SQL 函数 函数大全 - EWang c# ToString() 格式化字符串 - EWang 常用CSS样式 - EWang - 博客园 GridView空间的DataFormatString - EWang - 博客园 asp.net 学习资源列表 - EWang - 博客园 (转)Visual Studio .NET已检测到指定的WEB服务运行的不是ASP.NET 1.1版。你将无法运行ASP.NET WEB应用程序或服务 - EWang 一个困扰我半天的问题解决了,java环境真的很难配置 - EWang - 博客园 Liunx下常用程序(转) - EWang - 博客园 liunx下的一些常用命令 - EWang - 博客园 Eclipse 运行命令行参数大全 - EWang - 博客园
SQL INNER JOIN/OUTER JOIN - EWang
EWang · 2008-03-29 · via 博客园 - EWang

 INNER JOIN

For this tutorial, we will need to introduce another table, the Orders table in the Northwind database.  Below is a snapshot of the Orders table. (we will use only the first 9 records)

OrderID

CustomerID

EmployeeID

OrderDate

RequiredDate

ShippedDate

ShipVia

Freight

10248

VINET

5

7/4/1996

8/1/1996

7/16/1996

3

32.38

10249

TOMSP

6

7/5/1996

8/16/1996

7/10/1996

1

11.61

10250

HANAR

4

7/8/1996

8/5/1996

7/12/1996

2

65.83

10251

VICTE

3

7/8/1996

8/5/1996

7/15/1996

1

41.34

10252

SUPRD

4

7/9/1996

8/6/1996

7/11/1996

2

51.3

10253

HANAR

3

7/10/1996

7/24/1996

7/16/1996

2

58.17

10254

CHOPS

5

7/11/1996

8/8/1996

7/23/1996

2

22.98

10255

RICSU

9

7/12/1996

8/9/1996

7/15/1996

3

148.33

10256

WELLI

3

7/15/1996

8/12/1996

7/17/1996

2

13.97

10257

HILAA

4

7/16/1996

8/13/1996

7/22/1996

3

81.91

10258

VINET

99

7/1/1996

2/1/1996

4/2/1996

1

77.1

Looking at the Orders table above, notice that in the third column, EmployeeID is used. 
While it wouldn't be tough to lookup who employeeID 5 is in the employees table, this will become tedious and inefficient over time.  What we need is a way to link the two tables together, this is where we would use a join.

In the SQL-92 standard, inner joins can be specified in either the FROM or WHERE clause. This is the only type of join that SQL-92 supports in the WHERE clause. Inner joins 
specified in the WHERE clause are known as old-style inner joins.

INNER JOIN (definition): A join that displays only the rows that have a match in both joined tables.

SELECT field1, field2, field3
FROM table1 INNER JOIN table2
           ON
table1.field= table2.field

Example: We want to show which employee these orders belong to.
              (Note that in the SELECT section we must specify which table a field comes from.)

SELECT Employees.LastName, Employees.FirstName, Orders.OrderID
FROM Employees INNER JOIN Orders
           ON
Employees.EmployeeID = Orders.EmployeeID

Result: (Notice that OrderID 10258 does not show up in the results.  This is due to no
             matching EmployeeID 99 in the employees table.)

LastName

FirstName

OrderID

Buchanan

Steven

10248

Suyama

Michael

10249

Peacock

Margaret

10250

Leverling

Janet

10251

Peacock

Margaret

10252

Leverling

Janet

10253

Buchanan

Steven

10254

Dodsworth

Anne

10255

Leverling

Janet

10256

Peacock

Margaret

10257

Looking at the last SQL statement, it was somewhat long to type, especially if we want to select more columns.  One way to shorten the typing and make the script more readable is by using a table alias.

You define a table alias in the FROM clause.  We used e as the alias for employee and o as the alias for Orders.  Rewriting the above script using a table alias, we get:

SELECT e.LastName, e.FirstName, o.OrderID
FROM Employees e INNER JOIN Orders o
        ON e.EmployeeID = o.EmployeeID

Running the script again, we should get the same results.

LastName

FirstName

OrderID

Buchanan

Steven

10248

Suyama

Michael

10249

Peacock

Margaret

10250

Leverling

Janet

10251

Peacock

Margaret

10252

Leverling

Janet

10253

Buchanan

Steven

10254

Dodsworth

Anne

10255

Leverling

Janet

10256

Peacock

Margaret

10257

LEFT OUTER JOIN

LEFT OUTER JOIN (definition): All rows from the first-named table (the "left" table, which appears leftmost in the JOIN clause) are included. Unmatched rows in the
right table do not appear.

An OUTER JOIN is a join that includes rows even if they do not have related rows in the joined table. You can create three variations of an outer join to specify the unmatched rows to be included:

A LEFT JOIN is the same as a LEFT OUTER JOIN depending on the database.

LEFT OUTER JOIN (usage)

SELECT field1, field2, field3
FROM table1 LEFT OUTER JOIN table2
        ON
table1.field= table2.field

Example: This is basically the same query we used for the INNER JOIN example.
            

SELECT e.LastName, e.FirstName, o.OrderID
FROM Employees e LEFT OUTER JOIN Orders o
        ON e.EmployeeID = o.EmployeeID

Result: Notice that the LEFT OUTER JOIN vs the INNER JOIN

LEFT OUTER JOIN

INNER JOIN

LastName

FirstName

OrderID

Davolio

Nancy

Fuller

Andrew

Callahan

Laura

Smith

John

LastName

FirstName

OrderID

Buchanan

Steven

10248

Buchanan

Steven

10248

Suyama

Michael

10249

Suyama

Michael

10249

Peacock

Margaret

10250

Peacock

Margaret

10250

Leverling

Janet

10251

Leverling

Janet

10251

Peacock

Margaret

10252

Peacock

Margaret

10252

Leverling

Janet

10253

Leverling

Janet

10253

Buchanan

Steven

10254

Buchanan

Steven

10254

Dodsworth

Anne

10255

Dodsworth

Anne

10255

Leverling

Janet

10256

Leverling

Janet

10256

Peacock

Margaret

10257

Peacock

Margaret

10257

The LEFT OUTER JOIN returned results for ALL employees, even one without orders.

RIGHT OUTER JOIN

RIGHT OUTER JOIN (definition): All rows in the second-named table (the "right" table, which appears rightmost in the JOIN clause) are included. Unmatched rows in the left table are not included.

An OUTER JOIN is a join that includes rows even if they do not have related rows in the joined table. You can create three variations of an outer join to specify the unmatched rows to be included:

A RIGHT JOIN is the same as a RIGHT OUTER JOIN depending on the database.

RIGHT OUTER JOIN (usage)

SELECT field1, field2, field3
FROM table1 RIGHT OUTER JOIN table2
        ON
table1.field= table2.field

Example: This is basically the same query we used for the INNER JOIN example.
            

SELECT e.LastName, e.FirstName, o.OrderID
FROM Employees e RIGHT OUTER JOIN Orders o
        ON e.EmployeeID = o.EmployeeID

Result: Notice that the RIGHT OUTER JOIN vs the INNER JOIN

RIGHT OUTER JOIN

INNER JOIN

LastName

FirstName

OrderID

Buchanan

Steven

10248

LastName

FirstName

OrderID

Suyama

Michael

10249

Buchanan

Steven

10248

Peacock

Margaret

10250

Suyama

Michael

10249

Leverling

Janet

10251

Peacock

Margaret

10250

Peacock

Margaret

10252

Leverling

Janet

10251

Leverling

Janet

10253

Peacock

Margaret

10252

Buchanan

Steven

10254

Leverling

Janet

10253

Dodsworth

Anne

10255

Buchanan

Steven

10254

Leverling

Janet

10256

Dodsworth

Anne

10255

Peacock

Margaret

10257

Leverling

Janet

10256

10258

Peacock

Margaret

10257

The Right OUTER JOIN returned results for OrderID 10258 even though it did not have a corresponding employee.

The RIGHT and LEFT Joins are basically the same, the only thing matters is the order of the tables. 

FULL OUTER JOIN

FULL OUTER JOIN (definition): All rows in all joined tables are included, whether they
are matched or not.

An OUTER JOIN is a join that includes rows even if they do not have related rows in the joined table. You can create three variations of an outer join to specify the unmatched rows
to be included:

A FULL JOIN is the same as a FULL OUTER JOIN depending on the database.

FULL OUTER JOIN (usage)

SELECT field1, field2, field3
FROM table1 FULL OUTER JOIN table2
        ON
table1.field= table2.field

Example: This is basically the same query we used for the INNER JOIN example.
            

SELECT e.LastName, e.FirstName, o.OrderID
FROM Employees e FULL OUTER JOIN Orders o
        ON e.EmployeeID = o.EmployeeID

Result: Notice that the FULL OUTER JOIN vs the INNER JOIN

FULL OUTER JOIN

INNER JOIN

LastName

FirstName

OrderID

Davolio

Nancy

Fuller

Andrew

Callahan

Laura

Smith

John

Buchanan

Steven

10248

LastName

FirstName

OrderID

Suyama

Michael

10249

Buchanan

Steven

10248

Peacock

Margaret

10250

Suyama

Michael

10249

Leverling

Janet

10251

Peacock

Margaret

10250

Peacock

Margaret

10252

Leverling

Janet

10251

Leverling

Janet

10253

Peacock

Margaret

10252

Buchanan

Steven

10254

Leverling

Janet

10253

Dodsworth

Anne

10255

Buchanan

Steven

10254

Leverling

Janet

10256

Dodsworth

Anne

10255

Peacock

Margaret

10257

Leverling

Janet

10256

10258

Peacock

Margaret

10257

The FULL OUTER JOIN returned unmatched results for both tables.  Depending on the database unmatched records will be displayed with a "blank" or a <null>.

CROSS JOIN

CROSS JOIN (definition): A cross join returns the Cartesian product of the sets of rows from the joined tables.  Basically all the possible combinations from table1 and table2.

Take for example Table1,

EmployeeID FirstName
1 John
2 Mary
3 Bill
4 Larry

CROSS JOIN with Table2.

DeptID DeptName
100 Finance
200 Manufacturing
300 Customer Service

The result would be:

EmployeeID FirstName DeptID DeptName
1 John       100 Finance                  
2 Mary       100 Finance                  
3 Bill       100 Finance                  
4 Larry      100 Finance                  
1 John       200 Manufacturing            
2 Mary       200 Manufacturing            
3 Bill       200 Manufacturing            
4 Larry      200 Manufacturing            
1 John       300 Customer Service         
2 Mary       300 Customer Service         
3 Bill       300 Customer Service         
4 Larry      300 Customer Service         

Cross Joins are used to generate all possible combinations of records from tables that do not share a common element. If Table1 and Table2 are two sets then cross join = Table1 X Table2. 

Just remember that a CROSS JOIN with large tables could cause your database to come to a screeching halt.  Notice that table1 has 4 records and table2 has 3 records.  The cross join produced 12 rows (3x4=12).  Imagine if both tables had thousands or millions of records... The result would be very large.

For more imformation please click

http://sql.1keydata.com/cn/