Skip to content
Advertisement

INSERT or UPDATE from another table in mysql

I have two tables, with same schema –

I want to insert values from test2 table into test1, but if the row with same primary key already exist, update it. I know in mysql you can do similar thing with ON DUPLICATE KEY UPDATE like –

But I dont know how to do the above query with a SELECT from another table. What I am looking for is a query of form –

This query is obviously invalid. I would appreciate if somebody can make a valid query out of it.

Advertisement

Answer

This should work for you:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement