图解SQL的各种连接(Inner join,outer join,left join,right join)
2016-01-07 16:43
555 查看
由于 SQL Join 似乎被默认为基础,同时利用 ‘文氏图表’ 解释它,乍一看似乎是很自然的选择。然而,就像文章下面说的,我也发现在实际测试中,文氏图并没有完全符合SQL Join 语法。
通过图文并茂的方式对SQL的Join进行简单的介绍:join大致分为以下七种情况:
GO
CREATE TABLE [dbo].[test_a] (
[id] int NULL ,
[name] varchar(255) NULL
)
GO
-- ----------------------------
-- Records of test_a
-- ----------------------------
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'1', N'苹果')
GO
GO
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'2', N'橘子')
GO
GO
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'3', N'菠萝')
GO
GO
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'4', N'香蕉')
GO
GO
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'5', N'西瓜')
GO
GO
-----------------------------------------------------------
DROP TABLE [dbo].[test_b]
GO
CREATE TABLE [dbo].[test_b] (
[id] int NULL ,
[name] varchar(255) NULL
)
GO
-- ----------------------------
-- Records of test_b
-- ----------------------------
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'1', N'梨子')
GO
GO
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'2', N'苹果')
GO
GO
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'3', N'草莓')
GO
GO
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'4', N'桃子')
GO
GO
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'5', N'香蕉')
GO
GO
2示例介绍
产生A和B的交集。
SELECT
*
FROM
test_a
INNER JOIN test_b ON test_a.name = test_b.name
SELECT
*
FROM
test_a
FULL OUTER JOIN test_b ON test_a.name = test_b.name
SELECT
*
FROM
test_a
LEFT OUTER JOIN test_b ON test_a.name = test_b.name
产生在A表中有而在B表中没有的集合。
SELECT
*
FROM
test_a
LEFT OUTER JOIN test_b ON test_a.name = test_b.name
WHERE
test_b.name IS NULL
SELECT
*
FROM
test_a
RIGHT OUTER JOIN test_b ON test_a.name = test_b.name
SELECT
*
FROM
test_a
RIGHT OUTER JOIN test_b ON test_a.name = test_b.name
WHERE
test_a.name IS NULL
SELECT
*
FROM
test_a
FULL OUTER JOIN test_b ON test_a.name = test_b.name
WHERE
test_a.name IS NULL
OR test_b.name IS NULL
内容更新于:2017-02-17
通过图文并茂的方式对SQL的Join进行简单的介绍:join大致分为以下七种情况:
1准备数据
DROP TABLE [dbo].[test_a]GO
CREATE TABLE [dbo].[test_a] (
[id] int NULL ,
[name] varchar(255) NULL
)
GO
-- ----------------------------
-- Records of test_a
-- ----------------------------
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'1', N'苹果')
GO
GO
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'2', N'橘子')
GO
GO
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'3', N'菠萝')
GO
GO
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'4', N'香蕉')
GO
GO
INSERT INTO [dbo].[test_a] ([id], [name]) VALUES (N'5', N'西瓜')
GO
GO
-----------------------------------------------------------
DROP TABLE [dbo].[test_b]
GO
CREATE TABLE [dbo].[test_b] (
[id] int NULL ,
[name] varchar(255) NULL
)
GO
-- ----------------------------
-- Records of test_b
-- ----------------------------
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'1', N'梨子')
GO
GO
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'2', N'苹果')
GO
GO
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'3', N'草莓')
GO
GO
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'4', N'桃子')
GO
GO
INSERT INTO [dbo].[test_b] ([id], [name]) VALUES (N'5', N'香蕉')
GO
GO
2示例介绍
A.Inner join
产生A和B的交集。SELECT
*
FROM
test_a
INNER JOIN test_b ON test_a.name = test_b.name
B. Full outer join
产生A和B的并集。对于没有匹配的记录,则以null做为值。SELECT
*
FROM
test_a
FULL OUTER JOIN test_b ON test_a.name = test_b.name
C.Left outer join
产生表A的完全集,而B表中匹配的则有值,没匹配的以null值取代。SELECT
*
FROM
test_a
LEFT OUTER JOIN test_b ON test_a.name = test_b.name
D. Left outer join on where
产生在A表中有而在B表中没有的集合。SELECT
*
FROM
test_a
LEFT OUTER JOIN test_b ON test_a.name = test_b.name
WHERE
test_b.name IS NULL
E. RIGHT OUTER JOIN
产生表B的完全集,而A表中匹配的则有值,没匹配的以null值取代。SELECT
*
FROM
test_a
RIGHT OUTER JOIN test_b ON test_a.name = test_b.name
F. right outer join on where
产生在B表中有而在A表中没有的集合。SELECT
*
FROM
test_a
RIGHT OUTER JOIN test_b ON test_a.name = test_b.name
WHERE
test_a.name IS NULL
G. FULL OUTER JOIN WHERE
产生(A表中有但B表没有)和(B表中有但A表中没有)的数据集。SELECT
*
FROM
test_a
FULL OUTER JOIN test_b ON test_a.name = test_b.name
WHERE
test_a.name IS NULL
OR test_b.name IS NULL
H. cross join
表A和表B的数据进行一个N*M的组合,即笛卡尔积(交差集)。一般来说,我们很少用到这个语法。因为这种集合的使用对于性能来说非常危险,尤其是表很大。这里就不做介绍了…内容更新于:2017-02-17