简体   繁体   English

从子查询中选择歧义列

[英]Selecting ambiguous column from subquery

Suppose we have the following query: 假设我们有以下查询:

       select c.a, c.b, c.d
        from
           (select * from 
             tab1 join tab2
             on tab1.id = tab2.id)c

I get the following error: ERROR: 42702: column reference "d" is ambiguous 我收到以下错误: ERROR: 42702: column reference "d" is ambiguous

How would I fix this? 我该如何解决? Can I do something like c.tab1.id ? 我可以做类似c.tab1.id事情吗?

You may write your query like: 您可以这样写查询:

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;

The error is happening because both tab1 and tab2 have a column called d . 发生错误是因为tab1tab2都有一个名为d的列。

The simplest solution is to eliminate the subquery: 最简单的解决方案是消除子查询:

select ?.a, ?.b, ?.d
from tab1 join
     tab2
     on tab1.id = tab2.id

The ? ? is a placeholder for the table where the column comes from. 是列来源表格的占位符。

If you need the subquery, then you should similarly use qualified column names instead of select * , so you identify where the columns are coming from. 如果需要子查询,则应类似地使用限定的列名,而不要使用select * ,以便确定列的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM