Bug #34382 Error when using ON DUPLICATE KEY UPDATE table1.field1 = table2.field1,....
Submitted: 7 Feb 2008 14:55 Modified: 7 Mar 2008 15:05
Reporter: Ben Nelson Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server: General Severity:S3 (Non-critical)
Version:5.0.32 OS:Any (Sorry none)
Assigned to: CPU Architecture:Any
Tags: ON DUPLICATE KEY UPDATE

[7 Feb 2008 14:55] Ben Nelson
Description:
When doing a mysql query like below:
"INSERT INTO table2 (field1,field2,field3,field4,field5,field6,field7) select table1.field1,table1.field2,table1.field3,table1,field4,table1,field5,table1,field6,table1.field7 from table1 group by table1.field1 ON DUPLICATE KEY UPDATE table2.field3 = table1.field3, table2.field4 = table1.field4, table2.field5 = table1.field5,table2.field6 = table1.field6,table2.field7 = table1.field7";

I have "table1" setup with an primary index of type "int" and "table2" has primary key of type varchar(45).  I am expecting this command to insert field1-field7 from table1 into table2.  I created "table2" from "table1" before using this statement so fields are matched perfectly.  When there is a duplicate key or value on table2.field1 found then I want the program to update table2 fields with any changes made to "field3-field7" on table1.  I get the error below when i run the statement.

mysql errors = Unknown column 'table1.field1' in 'field list'1054

I have triple checked both tables and the fields are named correctly in both, and in the mysql query

How to repeat:
"INSERT INTO table2 (field1,field2,field3,field4,field5,field6,field7) select table1.field1,table1.field2,table1.field3,table1,field4,table1,field5,table1,field6,table1.field7 from table1 group by table1.field1 ON DUPLICATE KEY UPDATE table2.field3 = table1.field3, table2.field4 = table1.field4, table2.field5 = table1.field5,table2.field6 = table1.field6,table2.field7 = table1.field7";

sorry for the number of fields I am using but wanted to give who ever is working on this an exact example of what I am doing.

Suggested fix:
I fixed this query by doing the following and I get the results that I want...

"INSERT INTO table2 (field1,field2,field3,field4,field5,field6,field7) select table1.field1,table1.field2,table1.field3,table1,field4,table1,field5,table1,field6,table1.field7 from table1 group by table1.field1 ON DUPLICATE KEY UPDATE table2.field3 = values(field3), table2.field4 = values(field4), table2.field5 = values(field5),table2.field6 = values(field6),table2.field7 = values(field7)";

NOTE: using values(table1.field3) will give the same error I started with
[7 Feb 2008 15:05] Valeriy Kravchuk
Thank you for a problem report. Please, try to repeat with a newer version, 5.0.51a. In case of the same problem, please, send a complete test case, with CREATE TABLE for both tables and some test data.
[8 Mar 2008 0:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".