【问题标题】:Error: column reference is ambiguous错误:列引用不明确
【发布时间】:2018-07-02 09:41:32
【问题描述】:

我有两个表,我想在不使用联接的情况下从中获取日期。

id ProductVersion   productName  productDate
1    p1.1            product1     2017-3-11
2    p1.2            product1     2017-3-11
3    p2.1            product2     2017-5-12
4    p2.2            product2     2017-5-12
5    p2.3            product2     2017-5-12
6    p3.1            product3     2017-11-21
7    p3.1            product3     2017-11-21

表2

tid  productVersion  comments status       AvailableDate
101    p1.1            Good     Sold          2017-3-11  
102    p1.1            Good     Available     2017-3-12
1009   p1.1            Good     Available     2017-3-12
4008   p3.1            Average  NA            2017-11-11
106    p3.2            Good     Sold          2017-5-14
6      p3.1            Average  Available     2017-11-12

如上所示,我有两张表。

我想从上面两个表中获取productVersion,productName,productDate,Comments,status 列详细信息。

SQL 查询(无连接):

select productversion t1,productName t1,productDate t1,comments t2,status t2 from table1 t1,table2 t2 
where t1.productVersion = t2.productversion

错误信息:

Error: column reference "productDate" is ambiguous.

任何输入?

【问题讨论】:

  • select productversion t1,productName t1,productDate t1,comments t2,status t2 - 为什么要将相同的列别名分配给多个列?为什么它们与表别名相同? - 你的意思是写select t1.productversion, t1.productName, t1.productDate, t2.comments, t2.status
  • 您已经标记了 Oracle 和 PostgreSQL——它们是两种不同的 RDBMS。你用的是哪个?

标签: oracle postgresql


【解决方案1】:

[TL;DR] 您的主要问题是,您似乎将表别名放在列名之后,而当它们应该作为列名的前缀来标识列属于哪个表时,需要列的别名。

您的查询相当于:

select productversion AS columnalias1,
       productName    AS columnalias2,
       productDate    AS columnalias3,
       comments       AS columnalias4,
       status         AS columnalias5
from   table1 t1,
       table2 t2 
where  t1.productVersion = t2.productversion

您所有的列别名都是t1t2,因此您将获得多个具有相同名称的列。我认为这不是您想要的,因为两个表都有一个 productVersion 列,因此查询解析器不知道您打算使用哪个。您可能希望列名之前的表别名来标识每列来自哪个表:

select t1.productversion,
       t1.productName,
       t1.productDate,
       t2.comments,
       t2.status
from   table1 t1,
       table2 t2 
where  t1.productVersion = t2.productversion

第二个问题是,虽然您说它是“没有连接”的查询,但您使用的是旧的 Oracle 逗号连接语法,并且您的查询可以使用 ANSI/ISO SQL 语法重写为具有完全相同的功能,并且相当于:

select t1.productversion,
       t1.productName,
       t1.productDate,
       t2.comments,
       t2.status
from   table1 t1
       INNER JOIN table2 t2 
       ON ( t1.productVersion = t2.productversion )

如果你想要没有连接的东西,那么使用UNION ALL:

SELECT productVersion,
       productName,
       productDate,
       NULL AS Comments,
       NULL AS status
FROM   table1
UNION ALL
SELECT productVersion,
       NULL AS productName,
       NULL AS productDate,
       Comments,
       status
FROM   table2

但它不会关联两个表中的值。

【讨论】:

    【解决方案2】:

    要引用特定的表列,请使用以下语法:

    table_name.column_name
    

    您的查询应该是:

    select t1.productversion, t1.productName, t1.productDate,
           t2.comments, t2.status
    from table1 as t1
    join table2 as t2 on t1.productVersion = t2.productversion
    

    【讨论】:

      猜你喜欢
      • 2015-01-07
      • 2018-01-26
      • 1970-01-01
      • 2012-05-31
      • 2013-05-29
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多