Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL key ON DUPLICATE KEY UPDATE column is ambiguous

Tags:

mysql

Have the following problem

INSERT INTO statistics_new (SELECT * FROM statistics)
    -> 
    -> ON DUPLICATE KEY UPDATE
    -> 
    -> end_date = IF(end_date < VALUES(end_date), VALUES(end_date), end_date);
ERROR 1052 (23000): Column 'end_date' in field list is ambiguous

When I tried to specify column and table aliases as it recommended in http://dev.mysql.com/doc/refman/5.0/en/insert-select.html I had the same result.

Server version: 5.5.24-0ubuntu0.12.04.1 (Ubuntu)

The following query runs without problem :

INSERT INTO statistics_new (SELECT * FROM statistics)
ON DUPLICATE KEY UPDATE
end_date = VALUES(end_date)
like image 853
user1802525 Avatar asked Dec 16 '22 17:12

user1802525


1 Answers

SQLFiddle example

Basically I think you want this:

INSERT INTO statistics_new (SELECT * FROM statistics)
ON DUPLICATE KEY UPDATE
statistics_new.end_date = if(statistics_new.end_date < statistics.end_date, 
                             statistics.end_date, statistics_new.end_date);
like image 193
juergen d Avatar answered Jan 13 '23 09:01

juergen d