これは私の質問です。基本的に、私はLEFT JOIN
を使用して値@Remote.ImportantVal
を取得したい:結合を残してワイルドカードが結果を返さない?
declare @Local table
(
LocalId varchar(15),
Match1 int,
Match2 varchar(5)
)
insert into @local select '12_012_112', 5, 'MATH'
insert into @local select '12_012_113', 5, 'MATH'
insert into @local select '12_012_114', 5, 'MATH'
declare @Remote table
(
RemoteId varchar(15),
ImportantVal varchar(20),
Match1 int,
Match2 varchar(5)
)
insert into @Remote select 'ABC0012_012_112', 'Important', 5, 'MATH'
insert into @Remote select 'ABC0112_012_113', 'Important', 5, 'MATH'
insert into @Remote select 'ABC0012_012_114', 'Important', 5, 'MATH'
select l.localId, r.RemoteId, r.ImportantVal
from @Local l left join @Remote r on 'ABC%' + l.LocalId like r.RemoteId
and l.Match1 = r.Match1
and r.Match2 = r.Match2
問題が%
あるようだが、2つの文字後ABCが同じになることはありませんので、それが必要です。前のクエリでは、2つのテーブルが一致せず、3つの空の行が返されます。
私はこのようなものを使用している場合、それは基本的にinner join
なので、その後、唯一の2行は、一致:
select l.localId, r.RemoteId, r.ImportantVal
from @Local l left join @Remote r on 'ABC00' + l.LocalId like r.RemoteId
and l.Match1 = r.Match1
and r.Match2 = r.Match2
私もwhere
句でleft join
を試してみたが、それはただinner join
に変換そう、何も返さないです:
select l.localId, r.RemoteId, r.ImportantVal
from @Local l left join @Remote r on 'ABC%' + l.LocalId like r.RemoteId
where l.Match1 = r.Match1
and r.Match2 = r.Match2
がleft join
と%
を使用して、私は両方のテーブルのすべての3つの行を一致させることができますか?
ありがとうございました。
感謝。意外にも、注文を変更することができました。 – rbhat
これは何らかのバグですか?引数の順序のために結果セットが単純に変更されることは意味を成さない。 – rbhat
@rbhatup私が知っている限り、それは設計上のものであり、確かにバグではありません。オペレータがどのように定義されているかだけです。 – jpw