繁体   English   中英

在Join语句上添加Where子句

[英]Adding a Where Clause on The Join statement

我曾尝试在连接语句中的ON处添加一个where子句,但是我收到语法错误,我不确定将其放在哪里,因为我需要它从名为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

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

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

感谢已经给出的答案,但是我找到了正确的方法,我也从代码底部丢失了另一条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行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM