I have two tables in my database, table1 and table2. They are identical. But sometimes I change the data in table1.
How do I copy the data from table1 and update table2 to look the same?
I tried REPLACE INTO table2 SELECT * FROM table1 but it works like INSERT and just make new rows instead of updating the existing ones.
For
REPLACE INTOto work as intended, the destination table must have a primary key defined, otherwise MySQL cannot determine whether a row already exists and always assumes a new row. As a result, for tables without a primary key,REPLACE INTOacts exactly likeINSERT INTO.Alternatively, you can use two queries, one
UPDATEand oneINSERT, with appropriateWHERE (NOT) EXISTSclauses. The advantage of this is that it’s portable (REPLACE INTOis a MySQL extension).