Person_Tbl
PersonID | Name
------------------
101 | Stefan
102 | Andreas
PersonDescription_Tbl
PDId | PersonId | Desc | Lang
---------------------------
1 | 101 | Hello | en
2 | 101 | Hallo | de
3 | 102 | Hello | en
4 | 102 | Hallo | de
我正在寻找的输出是比较上面 2 个表并得到类似如下所示的结果:
输出
Name | Desc_en | Desc_de
---------------------------
Stefan | Hello | Hallo
Andreas | Hello | Hallo
我尝试了以下 SQL 子查询,但没有运气。如果有任何查询可以获取与上述类似的信息,请告诉我。
Select * from Person_Tbl where PersonID in (Select PersonID from PersonDescription_Tbl)
最佳答案
joins
使用 conditional 聚合,而不是使用 in
select p.Name, max(case when pd.Lang = 'en' then pd.Dec end) as Desc_en,
max(case when pd.Lang = 'de' then pd.Dec end) as Desc_de
from Person_Tbl p
inner join PersonDescription_Tbl pd on pd.PersonId = p.PersonId
group by p.Name;
关于用于合并表并产生结果的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49875108/