单个查询中的双左连接

问题描述 投票:0回答:1

我想知道是否有更好的方法来做到这一点。我知道如何通过多个步骤获得最终结果。

我有 2 个表,其中 SQL Server 数据库中有汽车信息。

第一个看起来像这样并存储在我们的数据库中

表名称:

Ours

Dealer  RegID   Feature       Included 
--------------------------------------
CarsRus 312904  A41:Insured   1
CarsRus 292965  A41:Insured   1
CarsRus 146278  A41:Insured   0
CarsRus 660838  A41:Insured   0
CarsRus 216326  A41:Insured   1

ChpCars 312904  A41:Insured   0
ChpCars 292965  A41:Insured   1
ChpCars 146278  A41:Insured   1
ChpCars 660838  A41:Insured   0
ChpCars 216326  A41:Insured   0

AutoMob 312904  A41:Insured   0
AutoMob 292965  A41:Insured   1
AutoMob 146278  A41:Insured   1
AutoMob 660838  A41:Insured   1
AutoMob 216326  A41:Insured   0

CarsRus 312904  M38:Serviced   0
CarsRus 292965  M38:Serviced   0
CarsRus 146278  M38:Serviced   0
CarsRus 660838  M38:Serviced   0
CarsRus 216326  M38:Serviced   1

ChpCars 312904  M38:Serviced   1
ChpCars 292965  M38:Serviced   0
ChpCars 146278  M38:Serviced   1
ChpCars 660838  M38:Serviced   1
ChpCars 216326  M38:Serviced   0

AutoMob 312904  M38:Serviced   1
AutoMob 292965  M38:Serviced   1
AutoMob 146278  M38:Serviced   1
AutoMob 660838  M38:Serviced   0
AutoMob 216326  M38:Serviced   1


CarsRus 312904  Y77:Cleaned   1
CarsRus 292965  Y77:Cleaned   0
CarsRus 146278  Y77:Cleaned   0
CarsRus 660838  Y77:Cleaned   1
CarsRus 216326  Y77:Cleaned   0

ChpCars 312904  Y77:Cleaned   0
ChpCars 292965  Y77:Cleaned   1
ChpCars 146278  Y77:Cleaned   1
ChpCars 660838  Y77:Cleaned   1
ChpCars 216326  Y77:Cleaned   0

AutoMob 312904  Y77:Cleaned   0
AutoMob 292965  Y77:Cleaned   0
AutoMob 146278  Y77:Cleaned   1
AutoMob 660838  Y77:Cleaned   1
AutoMob 216326  Y77:Cleaned   0

我们从合作伙伴那里得到的第二张表如下所示:

表名称:

Partner

RegID   Feature       Included
------------------------------
312904  A41:Insured   1
292965  A41:Insured   0
146278  A41:Insured   1
660838  A41:Insured   1
216326  A41:Insured   0

312904  M38:Serviced   0
292965  M38:Serviced   1
146278  M38:Serviced   0
660838  M38:Serviced   1
216326  M38:Serviced   0

312904  Y77:Cleaned   1
292965  Y77:Cleaned   0
146278  Y77:Cleaned   0
660838  Y77:Cleaned   1
216326  Y77:Cleaned   0

在第二张表中没有

Dealer
列,因为只有一个
Dealer
(合作伙伴)。

我想要做的是确定两个表所包含的功能之间的差异。

所以输出会是这样的:

Dealer  RegID   Feature       Ours  Partner
-------------------------------------------
CarsRus 292965  A41:Insured   1     0
CarsRus 146278  A41:Insured   0     1
CarsRus 660838  A41:Insured   0     1
CarsRus 216326  A41:Insured   1     0

ChpCars 312904  A41:Insured   0     1
ChpCars 292965  A41:Insured   1     0
ChpCars 660838  A41:Insured   0     1


AutoMob 312904  A41:Insured   0      1
AutoMob 292965  A41:Insured   1      0

CarsRus 292965  M38:Serviced   0      1
CarsRus 660838  M38:Serviced   0      1
CarsRus 216326  M38:Serviced   1      0

ChpCars 312904  M38:Serviced   1   0
ChpCars 292965  M38:Serviced   0   1
ChpCars 146278  M38:Serviced   1   0

AutoMob 312904  M38:Serviced   1   0
AutoMob 146278  M38:Serviced   1   0
AutoMob 660838  M38:Serviced   0   1
AutoMob 216326  M38:Serviced   1   0

ChpCars 312904  Y77:Cleaned   0   1
ChpCars 292965  Y77:Cleaned   1   0
ChpCars 146278  Y77:Cleaned   1   0

AutoMob 312904  Y77:Cleaned   0   1
AutoMob 146278  Y77:Cleaned   1   0

我可以使用如下所示的临时表来做到这一点

DECLARE @T AS TABLE
              (
                  Dealer varchar(50), 
                  RegID bigint, 
                  Feature varchar(50), 
                  Ours int, 
                  Partner int
              )

然后表

Ours
左连接到
Partner
,其中伙伴
RegID
为空,然后插入临时表;再次
Partner
左连接
Ours
并更新临时表

但这个过程并不是最好的方法。

我可以在单个查询中构建它吗?

sql sql-server tsql left-join
1个回答
0
投票

我认为你需要这样的查询:

select
  o.Dealer, o.RegID, o.Feature, o.Included as 'Ours', p.Included as 'Partner'
from ours o
  left join partner p on o.RegID = p.RegID and o.Feature = p.Feature
where
  o.Included != p.Included;

SQL Fiddle 演示

© www.soinside.com 2019 - 2024. All rights reserved.