将多个ID组合为变量SQL

时间:2016-08-08 12:13:50

标签: sql sql-server variables concatenation inner-join

撰写报告并遇到问题我希望有人可以指导我朝着正确的方向前进

我有2个表tbl_RegisterMain_HoldingLyndseyCouncils

tbl_RegisterMain_Holding中有12列,其中包含LA_id_Build (1 -12)

我希望能够使用变量(5,12,23)= ‘East Midlands’作为变量,它将带回任何具有这些数字的行。

以下是LyndseyCouncils表中的数据

LA_ID   Listings
5       East Midlands
12      East Midlands
23      East Midlands
15      East of England
21      East of England
25      East of England
79      London
80      London
201     London
3       London and the Home Counties
11      London and the Home Counties
13      London and the Home Counties
352     North East
365     North East
372     North East

这是我正在处理的查询

SELECT rm.Email,Forname,Surname,lc.listings
FROM dbo.tbl_RegisterMain_Holding AS rm
INNER JOIN [dbo].[LyndseyCouncils] AS lc
ON rm.LA_ID_live = lc.LA_ID
WHERE 
(rm.LA_ID_BUILD1 = @COUNCIL or rm.LA_ID_BUILD2 = @COUNCIL or rm.LA_ID_BUILD3 = @COUNCIL 
or rm.LA_ID_BUILD4 = @COUNCIL 
or rm.LA_ID_BUILD5 = @COUNCIL or rm.LA_ID_BUILD6 = @COUNCIL 
or rm.LA_ID_BUILD7 = @COUNCIL or rm.LA_ID_BUILD8 = @COUNCIL or rm.LA_ID_BUILD9 = @COUNCIL 
or rm.LA_ID_BUILD10 = @COUNCIL 
or rm.LA_ID_BUILD11 = @COUNCIL or rm.LA_ID_BUILD12 = @COUNCIL)

如果我只使用一个La_ID,但是因为我使用了多个LA_ID,它会工作它应该是IN() 但不知道我会怎么写, 也许我会以错误的方式解决这个问题。 任何帮助,将不胜感激。 感谢

0 个答案:

没有答案