Oracle 错误:列引用不明确

Oracle 错误:列引用不明确,oracle,postgresql,Oracle,Postgresql,我有两个表,我想从中获取日期而不使用联接 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.

我有两个表,我想从中获取日期而不使用联接

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查询(无联接):

错误消息:

Error: column reference "productDate" is ambiguous.

任何输入?

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

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

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

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

[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
select t1.productversion,
       t1.productName,
       t1.productDate,
       t2.comments,
       t2.status
from   table1 t1
       INNER JOIN table2 t2 
       ON ( t1.productVersion = t2.productversion )
并且所有列别名都是
t1
t2
,因此您将得到多个同名列。我认为这不是您想要的,因为两个表都有一个
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 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
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

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

[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
select t1.productversion,
       t1.productName,
       t1.productDate,
       t2.comments,
       t2.status
from   table1 t1
       INNER JOIN table2 t2 
       ON ( t1.productVersion = t2.productversion )
并且所有列别名都是
t1
t2
,因此您将得到多个同名列。我认为这不是您想要的,因为两个表都有一个
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 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
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

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

选择productversion t1、productName t1、productDate t1、comments t2、status t2
-为什么要为多个列指定相同的列别名?为什么它们与表别名相同您是否打算编写
选择t1.productversion、t1.productName、t1.productDate、t2.comments、t2.status
?您标记了Oracle和PostgreSQL-这是两种不同的RDBMS。您使用的是哪种?
选择productversion t1、productName t1、productDate t1、comments t2、status t2
-为什么要为多个列指定相同的列别名?为什么它们与表别名相同您是否打算编写
选择t1.productversion、t1.productName、t1.productDate、t2.comments、t2.status
?您标记了Oracle和PostgreSQL-这是两种不同的RDBMS。你用哪一种?