Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql on duplicate key update + sub query

Tags:

mysql

upsert

Using the answer from this question: Need MySQL INSERT - SELECT query for tables with millions of records

new_table
    * date
    * record_id (pk)
    * data_field


INSERT INTO new_table (date,record_id,data_field)
    SELECT date, record_id, data_field FROM old_table
        ON DUPLICATE KEY UPDATE date=old_table.data, data_field=old_table.data_field;

I need this to work with a group by and join.. so to edit:

INSERT INTO new_table (date,record_id,data_field,value)
    SELECT date, record_id, data_field, SUM(other_table.value) as value FROM old_table JOIN other_table USING(record_id) GROUP BY record_id
        ON DUPLICATE KEY UPDATE date=old_table.data, data_field=old_table.data_field, value = value;

I can't seem to get the value updated. If I specify old_table.value I get a not defined in field list error.

like image 789
jwzk Avatar asked May 02 '10 21:05

jwzk


2 Answers

Per the docs at http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

In the values part of ON DUPLICATE KEY UPDATE, you can refer to columns in other tables, as long as you do not use GROUP BY in the SELECT part. One side effect is that you must qualify nonunique column names in the values part.

So, you cannot use the select query because it has a group by statement. You need to use this trick instead. Basically, this creates a derived table for you to query from. It may not be incredibly efficient, but it works.

INSERT INTO new_table (date,record_id,data_field,value)
    SELECT date, record_id, data_field, value 
    FROM (
        SELECT date, record_id, data_field, SUM(other_table.value) as value 
        FROM old_table
        JOIN other_table
        USING(record_id)
        GROUP BY record_id
    ) real_query 
ON DUPLICATE KEY
    UPDATE date=real_query.date, data_field=real_query.data_field, value = real_query.value;
like image 95
Justin Noel Avatar answered Oct 21 '22 03:10

Justin Noel


While searching around some more, I found a related question: "MySQL ON DUPLICATE KEY UPDATE with nullable column in unique key".

The answer is that VALUES() can be used to refer to column "value" in the select sub-query.

like image 20
jwzk Avatar answered Oct 21 '22 03:10

jwzk