假设我们有以下查询:
select c.a, c.b, c.d
from
(select * from
tab1 join tab2
on tab1.id = tab2.id)c
我收到以下错误:ERROR: 42702: column reference "d" is ambiguous
我将如何解决这个问题?我可以做类似c.tab1.id
的事情吗?
您可以像这样编写查询:
SELECT a, b, d FROM (SELECT tab1.a AS a, tab1.b AS b, tab2.c AS c, tab2.d AS d
FROM tab1 JOIN tab2 ON tab1.id = tab2.id)c;
发生错误是因为tab1
和tab2
都有一个名为d
的列。
最简单的解决方案是消除子查询:
select ?.a, ?.b, ?.d
from tab1 join
tab2
on tab1.id = tab2.id
?
是列来源的表的占位符。
如果需要子查询,则类似地应使用限定列名而不是select *
,以便确定列的来源。