Nexus9988
Nexus9988

Reputation: 79

MYSQL - Upsert from one table to another

I am trying to use upsert to update and or insert from another table

INSERT INTO table1 (
  `uniqueCol1`, 
  `uniqueCol2`, 
  `created`, 
  `dataCol`
) 
VALUES (
   1, 
   t1.uniqueCol2Value, 
   NOW(), 
   t1.dataColValue 
) 
ON DUPLICATE KEY UPDATE
   `dataCol` = t1.dataColValue

Now from what I can tell I don't see how I can add what I think should be FROM table2 t1 into this to grab the values and put them into table1

Upvotes: 0

Views: 232

Answers (1)

Michel
Michel

Reputation: 571

I would suggest:

INSERT INTO table1 (
  `uniqueCol1`, 
  `uniqueCol2`, 
  `created`, 
  `dataCol`
) 
SELECT 1, uniqueCol2Value, NOW(), dataColValue FROM table2

Upvotes: 1

Related Questions