我遇到了一个问题:
table1
C1 C2 C3 tempId
1 4 5 ab
2 6 7 fc
3 8 9 vb
表2
ids val
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
我想传递tempId的值,即ab,并希望输出像
valofc1 valofc2 valofc3
a d e
请帮助我不知道如何实现这一点。
答案 0 :(得分:4)
尝试这种方式:
select t21.val as valofc1, t22.val as valofc2, t23.val as valofc3
from table1 as t
join table2 as t21 on t21.ids = t.C1
join table2 as t22 on t22.ids = t.C2
join table2 as t23 on t23.ids = t.C3
where t.tempId = 'ab'
答案 1 :(得分:1)
试试这个
select t2.val valofc1,t3.val valofc2,t4.val valofc3 from table1 t1
inner join table2 t2 on t1.C1 = t2.ids
inner join table2 t3 on t1.C2 = t3.ids
inner join table2 t4 on t1.C3 = t4.ids
where tempId = 'ab'
答案 2 :(得分:0)
Declare @t table (C1 int,C2 int,C3 int,tempId varchar(50))
insert into @t values (1,4,5,'ab'),(2,6,7,'fc'),(3,8,9,'vb')
Declare @table2 table (id int,val varchar(50))
insert into @table2 values(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'),(6,'f'),(7,'g'),(8,'h'),(9,'i')
select
(select val from @table2 where id=t.C1)valofc1 ,
(select val from @table2 where id=t.C2)valofc2,
(select val from @table2 where id=t.C3)valofc3
from @t t where tempid='ab'