我面臨着一個問題:帶有多列的SQL查詢加入
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
請幫助我不要不知道如何實現這一點。
我面臨着一個問題:帶有多列的SQL查詢加入
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
請幫助我不要不知道如何實現這一點。
試試這個
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'
歡迎您:)! –
試試這個方法:
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'
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'
在RDBMS
?????是否適用於SQL Server或MYSQL或SQLIte – Dhaval