这是我的表格:
id referencePerson1ID referencePerson2ID referencePerson3ID
1 1 2 3
T2 referencePerson:
id name
1 Peter
2 John
3 Mary
当我选择T1 id =1
时,我希望得到以下结果:
id referencePerson1 referencePerson2 referencePerson3
1 Peter John Mary
我该怎么做? 感谢
答案 0 :(得分:1)
这是做事的一种基本方式:
SELECT T1.id, P1.name, P2.name, p3.name
FROM record T1 LEFT JOIN referencePerson P1
ON T1.referencePerson1ID=P1.id
LEFT JOIN referencePerson P2
ON T1.referencePerson2ID=P2.id
LEFT JOIN referencePerson P3
ON T1.referencePerson3ID=P3.id
当人数未知时,可以使用PIVOT
完成另一种方式答案 1 :(得分:0)
你可以连续三次加入:
SELECT t1.id, t21.name, t22.name, t23.name
FROM T1
INNER JOIN T2 t21 ON t21.id = T1.referencePerson1ID
INNER JOIN T2 t22 ON t22.id = T1.referencePerson2ID
INNER JOIN T2 t23 ON t23.id = T1.referencePerson3ID
WHERE T1.id = 1