简体   繁体   中英

Error Code: 1052. Column 'TradeDate' in field list is ambiguous

I get an error code like this

Error Code: 1052. Column 'TradeDate' in field list is ambiguous

when I try to join two tables. The two csv files columns look like this: data_price_temp consumption

So, I named the Date column in all tables like TradeDate. Then, I am trying to use TradeDate from two tables that I have created to perform a join

My syntax looks like this

CREATE DATABASE price_temp;

USE price_temp;

CREATE TABLE pricetemp(TradeDate CHAR(10),
                       Price double,
                       Temp double); 
SELECT* FROM pricetemp

CREATE TABLE consumption(TradeDate CHAR(10),
                        Consumption double);
                        
 SELECT* FROM consumption  
 
CREATE TABLE production(TradeDate CHAR(10),
                       Production double);
                       
SELECT * FROM production

SELECT TradeDate, Price, Temp, Consumption
FROM pricetemp, consumption
WHERE pricetemp.TradeDate = consumption.TradeDate`

your last query should use the explicit join syntax and should always have the table.field notation to avoid the error you are referring

SELECT pricetemp.TradeDate, Price, Temp, Consumption
FROM pricetemp JOIN consumption ON 
pricetemp.TradeDate = consumption.TradeDate

or

SELECT consumption.TradeDate, Price, Temp, Consumption
FROM pricetemp JOIN consumption ON 
pricetemp.TradeDate = consumption.TradeDate

To join another table just:

SELECT consumption.TradeDate, Price, Temp, Consumption, anotherTable.production
FROM pricetemp 
    JOIN consumption ON pricetemp.TradeDate = consumption.TradeDate
    JOIN anotherTable ON pricetemp.TradeDate = anotherTable.TradeDate

The Fix

SELECT pricetemp.TradeDate, Price, Temp, Consumption
FROM pricetemp, consumption
WHERE pricetemp.TradeDate = consumption.TradeDate`

Explanation

The error tells you that you have several fields having the same name. When you encounter this situation, you need to clarify which field you want to use via prepending the table's name, like tablename.fieldname . Once you understand this you will have no problem in joining further tables. In our specific case pricetemp.TradeDate and consumption.TradeDate have the very same value, but the RDBMS does not know this and asks you to specify which one you intend to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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