Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 左连接和通配符不返回结果?_Sql Server_Sql Server 2008_Tsql_Join - Fatal编程技术网

Sql server 左连接和通配符不返回结果?

Sql server 左连接和通配符不返回结果?,sql-server,sql-server-2008,tsql,join,Sql Server,Sql Server 2008,Tsql,Join,这是我的问题。本质上,我想通过使用左连接来检索value@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

这是我的问题。本质上,我想通过使用
左连接来检索value
@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
问题似乎是
%
,但它是必需的,因为ABC后面的2个字符永远不会相同。在上一个查询中,两个表不匹配,它返回3个空行

如果我使用这样的东西,那么只有两行匹配,因为它基本上是一个
内部连接

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
,但这只是将其转换为
内部连接,因此没有返回任何内容:

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
使用
左联接
%
,如何匹配两个表中的所有三行


谢谢。

更改join子句中参数的顺序:

from @Local l left join @Remote r on r.RemoteId like 'ABC%' + l.LocalId 
你应该得到你期望的三行。(至少我的测试给出了这一点)

必须在文档中所述的类似表达式右侧的模式中使用通配符:

匹配表达式[不]类似模式[转义字符]


您可以在
WHERE
中作为两个不同的子句进行连接

样本数据

CREATE TABLE #Local (LocalId varchar(15), Match1 int, Match2 varchar(5))
INSERT INTO #Local 
VALUES 
('12_012_112', 5, 'MATH')
,('12_012_113', 5, 'MATH')
,('12_012_114', 5, 'MATH')

CREATE TABLE #Remote (RemoteId varchar(15), ImportantVal varchar(20), Match1 int, Match2 varchar(5))   
INSERT INTO #Remote 
VALUES
('ABC0012_012_112', 'Important', 5, 'MATH')
,('ABC0112_012_113', 'Important', 5, 'MATH')
,('ABC0012_012_114', 'Important', 5, 'MATH')
实际查询

SELECT
l.LocalId
,r.RemoteId
,r.ImportantVal
FROM #Local l
LEFT JOIN #Remote r
ON r.RemoteId LIKE 'ABC%'
AND l.LocalID = RIGHT(r.RemoteId,LEN(l.LocalId))
AND l.Match1 = r.Match1
AND l.Match2 = r.Match2
为什么不只是:

select l.localId, r.RemoteId, r.ImportantVal
from @Local l left join @Remote r on RIGHT(l.LocalId,10) = r.RemoteId
and l.Match1 = r.Match1
and r.Match2 = r.Match2
或者,如果“ABC”很重要,那么

select l.localId, r.RemoteId, r.ImportantVal
from @Local l left join @Remote r on RIGHT(l.LocalId,10) = r.RemoteId
and l.LocalId like 'ABC%'
and l.Match1 = r.Match1
and r.Match2 = r.Match2

那是某种臭虫吗?仅仅因为参数的顺序而改变结果集是毫无意义的。@rbhatup据我所知,它是设计出来的,当然不是bug。这就是操作符的定义方式。