sql操作,已知多个id怎么用一条命令根据这些id查找出这些id对应的值
3个回答
展开全部
--不知道你的表结构,我只能这样回答了
--以下id和值不在同一个表,并且数据的存储结构也不同
create table M
(
id int,
name varchar(10)
)
Create Table N1
(
id int,
ids Varchar(10)
)
Create Table N2
(
id int,
id1 int,
id2 int,
id3 int,
id4 int
)
Create Table N3
(
id int,
name varchar(10),
ids int
)
insert into M values(1,'A')
insert into M values(2,'B')
insert into M values(3,'C')
insert into M values(4,'D')
insert into N1 values(1,'1,2,3')
insert into N1 values(2,'2,3,4')
insert into N2 values(1,1,1,2,2)
insert into N2 values(1,1,2,3,4)
insert into N3 values(1,'A',1)
insert into N3 values(2,'A',2)
insert into N3 values(3,'A',3)
insert into N3 values(4,'B',4)
insert into N3 values(5,'B',2)
insert into N3 values(6,'C',3)
--格式1
--思路,按逗号拆分字符后和M关联,然后按id再拼接name
With CT
As
(
select N1.id,M.name from N1
outer apply dbo.f_split(ids,',') A
left join M on A.col=M.id
)
select id,stuff((select ','+name from CT where id=A.id for xml path('')),1,1,'') from CT A
group by id
--格式2
--直接4个left join
select N2.id,A.name As A,B.name As B,C.name As C,D.name As D from N2
left join M A on N2.id1=A.id
left join M B on N2.id2=B.id
left join M C on N2.id3=C.id
left join M D on N2.id4=D.id
--格式3
--这个最简单
select N3.id,M.name As M from N3
left join M on N3.ids=M.id
--以下id和值再同一个表
--如果id是字符串,用id+逗号来判断,否则可能会发生误判
select * from table_name where charindex(id+',','id1,id2,id3,...')>0
--如果id是数值
select * from table_name where id in (1,2,3...)
展开全部
select * from table_name where id in (1,2,3...)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select * from table where instr('id1,id2,id3,...',id)>0
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询