如何在Sql Server中显示匹配的ID [英] How to show matching Id in Sql Server

查看:77
本文介绍了如何在Sql Server中显示匹配的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个表名ContactBook,其中包含两个字段ContactID和ItemID。这里我将ContactId与ItemID相关联,即,一个contactid与多个项目相关联。在sql server我想获取关联的contactid,如果我给项目ID,



表示

如果我给项目ID 1,2,3 ,然后我想获取与1和2以及3相关联的contactid,如果任何联系人与1或2相关联它不应该来。





Plz给我正确的建议

Hi,

I have one table name ContactBook, which contain two fields ContactID and ItemID. Here am associating ContactId to ItemID, i,e one contactid is associating with more than one item. In sql server i want to fetch associated contactid if i give item id,

means
if i give item id 1,2,3,then i want to fetch contactid which is associated with 1 and 2 and 3, if any contact is associated with 1 or 2 it should not come.


Plz give me proper suggestions

推荐答案

你好,



你可以计划像 -



you can plan like -
begin
declare @id1 varchar(50)=3
declare @id2 varchar(50)=0
declare @id3 varchar(50)=0
declare @id4 varchar(50)=0
declare @id5 varchar(50)=0
declare @counter int=0
begin
if(@id1!=0)
set @counter=1

if(@id1!=0 and @id2!=0)
set @counter=2

if(@id1!=0 and @id2!=0 and @id3!=0)
set @counter=3

if(@id1!=0 and @id2!=0 and @id3!=0 and @id4!=0)
set @counter=4

if(@id1!=0 and @id2!=0 and @id3!=0 and @id4!=0 and @id5!=0)
set @counter=5

if(@counter=0)
begin
select taskid
            from tbltask where updatedby=0
end
else if (@counter=1)
begin
select taskid
            from tbltask
            where updatedby=@id1
            end

else if (@counter=2)
begin
select taskid
            from tbltask
            where updatedby=@id1
            and updatedby=@id2
            end
else if (@counter=3)
begin
select taskid
            from tbltask
            where updatedby=@id1
            and updatedby=@id2
            and updatedby=@id3
            end
else if (@counter=4)
begin
select taskid
            from tbltask
            where updatedby=@id1
            and updatedby=@id2
            and updatedby=@id3
            and updatedby=@id4
            end
else if (@counter=5)
begin
select taskid
            from tbltask
            where updatedby=@id1
            and updatedby=@id2
            and updatedby=@id3
            and updatedby=@id4
            and updatedby=@id5
            end
        end
end


这篇关于如何在Sql Server中显示匹配的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆