我有两个表,我想在不使用joins的情况下从中获取日期。
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-21Table2
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.有什么建议吗?
发布于 2018-01-23 22:35:29
您的主要问题是,您似乎将表别名放在列名之后,而表别名应该作为列名的前缀,以标识列所属的表。
您的查询相当于:
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而且您所有的列别名都是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 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但它不会将两个表中的值关联起来。
发布于 2018-01-23 22:21:06
要引用特定的表列,请使用以下语法:
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.productversionhttps://stackoverflow.com/questions/48403938
复制相似问题