从多个表中选择列

时间:2016-02-28 19:36:11

标签: sql postgresql django-database

我有多个表具有相同的主键字段但没有其他相同的字段。我想从匹配特定主键的所有表中选择一组列,如下所示:

SELECT T1.a, T2.b, ..., TN.z FROM T1, T2, ..., TN WHERE T1.pk = "abc"

1 个答案:

答案 0 :(得分:2)

SELECT T1.a, T1.b, ..., TN.z
FROM T1 INNER JOIN T2 ON T1.pk = T2.pk
INNER JOIN T3 on T1.pk = T3.pk
...
INNER JOIN TN on T1.pk = TN.pk
AND TN.pk = "abc"

或以较短的形式(感谢@Abelisto!)。

SELECT T1.a, T1.b, ..., TN.z
FROM T1 JOIN T2 using(pk)
JOIN T3 using(pk)
...
JOIN TN using(pk)
WHERE TN.pk = "abc"