INSERT
Sometimes, there was an old data that need to be transfer from old table to new table. However, not all fields are required to transfer. So, below is the example of two tables and fields, and the sql statement.
tableOld
id name ic_no phone_no address
id name ic_no phone_no address
tableNew
id name ic_no phone_no
id name ic_no phone_no
MySQL statement
INSERT INTO tableNew (name,ic_no,phone_no) SELECT name,ic_no,phone_no FROM tableOld WHERE tableOld.id > 1;
This SQL statement is called as insert into select.
UPDATE
In this case, I want to get value from table1 to update into table2 based on certain condition or value.For example :-
table1
id name ic_no phone_no
id name ic_no phone_no
table2
id name phone_no address
id name phone_no address
MySQL statement
UPDATE table2 t2
INNER JOIN table1 t1
ON t2.name = t1.name
SET t2.ic_no= t1.ic_no
WHERE
t2.name = t1.name;