查询一个左表和两个右表UNION结果没有union运算符

时间:2016-03-04 06:42:47

标签: sql sql-server performance sql-server-2008

我试图将具有相同结构的两个表连接到具有相同连接条件的另一个表。

我也希望避免使用UNION,唯一的原因是人们试图不使用联盟,如果没有联盟,它可能是可行的。

这是一个返回所需结果的联合查询。

SELECT     Left_Table.KeyValue, Left_Table.Data_Left1,Left_Table.Data_Left2, UN.KeyValue AS KeyValue2, UN.Data_Right1, UN.Data_Right2
FROM         Left_Table LEFT OUTER JOIN
    (SELECT     KeyValue, Data_Right1, Data_Right2
    FROM          Right_Table_1
      UNION
    SELECT     KeyValue, Data_Right1, Data_Right2
    FROM Right_Table_2) AS UN ON Left_Table.KeyValue = UN.KeyValue

结果如下。

KeyValue    Data_Left1  Data_Left2  KeyValue2   Data_Right1 Data_Right2
1           Alpha       2016-01-23  1           Grumpy      1812-01-01 
2           beta        2016-01-24  2           Doc         1812-01-01 
2           beta        2016-01-24  2           Donna       2014-12-25 
3           gamma       2015-12-25  3           Rudolph     2015-12-25 

这是我尝试的另一种查询,但没有那么做。

SELECT     Left_Table.KeyValue, Left_Table.Data_Left1,Left_Table.Data_Left2, COALESCE (RT1.KeyValue, RT2.KeyValue) AS RightKeyvalue, COALESCE (RT1.Data_Right1, RT2.Data_Right1) AS Data_Right1, 
                  COALESCE (RT1.Data_Right2, RT2.Data_Right2) AS Data_Right2
FROM         Left_Table 
LEFT OUTER JOIN    Right_Table_1 AS RT1 ON Left_Table.KeyValue = RT1.KeyValue 
LEFT OUTER JOIN    Right_Table_2 AS RT2 ON Left_Table.KeyValue = RT2.KeyValue

结果

KeyValue    Data_Left1  Data_Left2  KeyValue2   Data_Right1 Data_Right2
1           Alpha       2016-01-23  1           Grumpy      1812-01-01 
2           beta        2016-01-24  2           Donna       2014-12-25 
3           gamma       2015-12-25  3           Rudolph     2015-12-25 

以下是一些用于测试的创建表和数据脚本。非常感谢您的帮助。

CREATE TABLE [dbo].[Right_Table_2](
    [KeyValue] [int] NULL,
    [Data_Right1] [nvarchar](50) NULL,
    [Data_Right2] [datetime] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Right_Table_2] ([KeyValue], [Data_Right1], [Data_Right2]) VALUES (1, N'Grumpy', CAST(0xFFFF827200000000 AS DateTime))
INSERT [dbo].[Right_Table_2] ([KeyValue], [Data_Right1], [Data_Right2]) VALUES (2, N'Doc', CAST(0xFFFF827200000000 AS DateTime))

CREATE TABLE [dbo].[Right_Table_1](
    [KeyValue] [int] NULL,
    [Data_Right1] [nvarchar](50) NULL,
    [Data_Right2] [datetime] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Right_Table_1] ([KeyValue], [Data_Right1], [Data_Right2]) VALUES (2, N'Donna', CAST(0x0000A40C00000000 AS DateTime))
INSERT [dbo].[Right_Table_1] ([KeyValue], [Data_Right1], [Data_Right2]) VALUES (3, N'Rudolph', CAST(0x0000A57900000000 AS DateTime))
INSERT [dbo].[Right_Table_1] ([KeyValue], [Data_Right1], [Data_Right2]) VALUES (4, N'Comet', CAST(0x0000A57900000000 AS DateTime))

CREATE TABLE [dbo].[Left_Table](
    [KeyValue] [int] NULL,
    [Data_Left1] [nvarchar](50) NULL,
    [Data_Left2] [datetime] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Left_Table] ([KeyValue], [Data_Left1], [Data_Left2]) VALUES (1, N'Alpha', CAST(0x0000A59600000000 AS DateTime))
INSERT [dbo].[Left_Table] ([KeyValue], [Data_Left1], [Data_Left2]) VALUES (2, N'beta', CAST(0x0000A59700000000 AS DateTime))
INSERT [dbo].[Left_Table] ([KeyValue], [Data_Left1], [Data_Left2]) VALUES (3, N'gamma', CAST(0x0000A57900000000 AS DateTime))

1 个答案:

答案 0 :(得分:1)

UNION操作相当于两个表的所有字段上的FULL OUTER JOIN

SELECT Left_Table.KeyValue, Left_Table.Data_Left1, Left_Table.Data_Left2, 
       UN.KeyValue AS KeyValue2, UN.Data_Right1, UN.Data_Right2
FROM Left_Table LEFT OUTER JOIN (
SELECT COALESCE(t1.KeyValue, t2.KeyValue) AS KeyValue, 
       COALESCE(t1.Data_Right1, t2.Data_Right1) AS Data_Right1,
       COALESCE(t1.Data_Right2, t2.Data_Right2) AS Data_Right2
FROM Right_Table_1 AS t1
FULL OUTER JOIN Right_Table_2 AS t2  
  ON t1.KeyValue = t2.KeyValue AND 
     t1.Data_Right1 = t2.Data_Right1 AND 
     t1.Data_Right2 = t2.Data_Right2) AS UN
  ON Left_Table.KeyValue = UN.KeyValue

我个人认为UNION操作更受欢迎,因为它不那么冗长,并且可能与FULL OUTER JOIN具有相同的性能。