MySQL键ON DUPLICATE KEY UPDATE列不明确

时间:2012-11-07 09:50:45

标签: mysql

有以下问题

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

当我尝试按照http://dev.mysql.com/doc/refman/5.0/en/insert-select.html中的建议指定列和表别名时,我得到了相同的结果。

服务器版本:5.5.24-0ubuntu0.12.04.1(Ubuntu)

以下查询运行没有问题:

INSERT INTO statistics_new (SELECT * FROM statistics)
ON DUPLICATE KEY UPDATE
end_date = VALUES(end_date)

3 个答案:

答案 0 :(得分:6)

SQLFiddle example

基本上我认为你想要这个:

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);

答案 1 :(得分:1)

statistics_newstatistics都有end_date列。请在SQL中区分它们。

答案 2 :(得分:0)

如果你试图在最后一行设置一个变量,那应该是:

SET @end_date = IF(end_date < VALUES(end_date), VALUES(end_date), end_date);