我有一个数据行,其中多个fk指向同一个表。我怎样才能产生这个:
表1
id | name | fk1 | fk2 | fk3
1 test EA US NULL
2 test2 Null UK US
表2
id | details
EA East Asia
US United States
UK United Kingdom
我想生成类似这样的东西
id | name | details
1 test East Asia
1 test United States
2 test2 United Kingdom
2 test2 United States
我一直在四处寻找,但可能我输错了搜索关键字或短语。
感谢
这就是我做的事情
select t1.id,t1.name,t2.details from table1 t1
left join table2 t2 on t2.id = t1.fk1
union
(select t1.id,t1.name,t2.details from table1 t1
left join table2 t2 on t2.id = t1.fk2
)
union
select t1.id,t1.name,t2.details from table1 t1
left join table2 t2 on t2.id = t1.fk3
但是此表生成的行为null
答案 0 :(得分:1)
使用UNPIVOT
将每个fk列作为单独的行。
select u.id, u.name, t2.details
from table1 t1
unpivot(
region for regions in (fk1, fk2, fk3)
) u
join table2 t2 on t2.id = u.region