在Join语句中添加Where子句

时间:2018-03-20 13:41:13

标签: sql tsql

我试过在我的连接语句中添加一个where子句,在ON, 但是,我收到语法错误,我不知道在哪里放这个,因为我需要它从一个名为systemlookup的表中提取数据

     DECLARE @OptionalModules TABLE (moduleid INT, name VarChar(200))
 INSERT INTO @OptionalModules
 SELECT CAST (LookupReference AS INT)
 FROM dbo.systemlookup
 left join @xml.nodes('//Modules/*') as organisation(license) on 
 organisation.license.value('local-name(.)', 'varchar(50)') =
 case LookupReference
 when '1' then 'a'
 when '2' then 'b'
 when '6' then 'c'
 when '8' then 'd'
 when '9' then 'e'
 when '10' then 'f'
 when '11' then 'g'
 when '12' then 'h'
 when '13' then 'i'
 when '14' then 'j'
 when '15' then 'k'
 when '16' then 'l'
 when '17' then 'm'
 when '18' then 'n'
 when '20' then 'o'
 when '21' then 'p'
 when '22' then 'q'
 when '23' then 'r'
 when '24' then 's'
 when '25' then 't'
 when '26' then 'u'
 when '27' then 'v'
 when '28' then 'w'
 when '29' then 'x'
 when '31' then 'y'
 when '32' then 'z'
 when '33' then 'aa'
 when '16016' then 'bb'
end

2 个答案:

答案 0 :(得分:0)

在ON子句后添加AND,语法如下:

SELECT A
FROM Table A
INNER JOIN Table B ON A.ID = B.ID AND A.ID2 = B.ID2

答案 1 :(得分:0)

感谢您的答案,但我找到了正确的方法,我还缺少代码底部的另一段SQL

             DECLARE @OptionalModules TABLE (moduleid INT, name VarChar(200))
     INSERT INTO @OptionalModules
     SELECT CAST (LookupReference AS INT)
     FROM dbo.systemlookup
     left join @xml.nodes('//Modules/*') as organisation(license) on 
     organisation.license.value('local-name(.)', 'varchar(50)') =
     case LookupReference
     when '1' then 'a'
     when '2' then 'b'
     when '6' then 'c'
     when '8' then 'd'
     when '9' then 'e'
     when '10' then 'f'
     when '11' then 'g'
     when '12' then 'h'
     when '13' then 'i'
     when '14' then 'j'
     when '15' then 'k'
     when '16' then 'l'
     when '17' then 'm'
     when '18' then 'n'
     when '20' then 'o'
     when '21' then 'p'
     when '22' then 'q'
     when '23' then 'r'
     when '24' then 's'
     when '25' then 't'
     when '26' then 'u'
     when '27' then 'v'
     when '28' then 'w'
     when '29' then 'x'
     when '31' then 'y'
     when '32' then 'z'
     when '33' then 'aa'
     when '16016' then 'bb'
    end
where s.LookupTypeId = 1 and cast(case license.value('.', 'varchar(3)') when 'Yes' then 1 when 'No' then 0 else 1 end as bit) = 1

我已将演员阵容放在底部,现在返回30行而不是原来的2340行。

相关问题