ORA-00904标识符和列歧义无效

时间:2014-01-20 00:13:22

标签: sql oracle oracle11g

我正在尝试运行以下sql查询:

select customers.cust_first_name, customers.cust_last_name, customers.cust_gender,
customers.cust_city, countries.country_name, products.prod_id
from customers
join countries on customers.cust_id = countries.country_id
join products on profits.prod_id = products.prod_id
join profits on customers.cust_id = profits.cust_id

但是我收到以下错误:

An error was encountered performing the requested operation:

ORA-00904: "PROFITS"."PROD_ID":Invalid identifier 
00904.00000-"%s: invalid Identifier" 

我正在使用oracle 11g版本。

您是否还可以指导我如何动态解决/处理列歧义问题,即是否有办法让数据库忽略列模糊问题?

1 个答案:

答案 0 :(得分:2)

您必须在from子句中命名该表,然后才能在on语句中使用它:

select customers.cust_first_name, customers.cust_last_name, customers.cust_gender,
       customers.cust_city, countries.country_name, products.prod_id
from customers join
     countries
     on customers.cust_id = countries.country_id join
     profits
     on customers.cust_id = profits.cust_id join
     products
     on profits.prod_id = products.prod_id;

如果您使用表别名,您的查询也会更具可读性:

select cu.cust_first_name, cu.cust_last_name, cu.cust_gender,
       cu.cust_city, co.country_name, pr.prod_id
from customers cu join
     countries co
     on cu.cust_id = co.country_id join
     profits pf
     on cu.cust_id = pf.cust_id join
     products pr
     on pf.prod_id = pr.prod_id;