It is not clear whether you want to know only the SQL commands or if you want to create the connection with a PHP script.
So, in a generic way,
First delete existing data:
DELETE FROM TAB2;
Then execute INSERT INTO TAB2 (SELECT * FROM TAB1);
You can do this by using a MySQL front-end like PHPMyAdmin.
*illustrativeimage
YoucannotdoyourqueriesatthesametimebecauseMySQLpreventsmultiplequeriesbydefault.Itispossibletochangethispatternbutitimpliessecurityissues.
It'simportanttonotethatifyoujustwanttoreplacethedatainonetablewiththedatainanothertable,youcouldonlyapplyREPLACEINTO,withoutusingtruncate
ordelete
.
REPLACEINTOTAB2(SELECT*FROMTAB1)
That'senough.
Butforaspecificcasewhereyouwanttoclear"traces" from the TAB2
table that do not exist in TAB1
, it's best to delete everything before proceeding with INSERT
.