I have 'Home' table to save all data from importing csv file to phpMyAdmin. At first, the table not containing any data and i import a csv file that contain field (home_id, name and count). The table have additional field 'status' default to 1 and home_id is set as unique key. Now the table 'Home' have data.
home_id | name | count | status
H1 | Apartment | 3 | 1
H2 | Condominium | 6 | 1
H3 | Cottage | 4 | 1
Next, i import same csv file but with updated value for 'count'. By using INSERT INTO....DUPLICATE KEY UPDATE, i manage to update row that have same home_id and set 'status' as 0.
home_id | name | count | status
H1 | Apartment | 8 | 0
H2 | Condominium | 9 | 0
H3 | Cottage | 4 | 1
But i want to save data before and after updated for example as below table.
home_id | name | count | status
H1 | Apartment | 3 | 1
H2 | Condominium | 6 | 1
H1 | Apartment | 8 | 0
H2 | Condominium | 9 | 0
H3 | Cottage | 4 | 1
I realize that i cannot set 'home_id' as unique key because it will not allowed to save same 'home_id'. Is there any way i can do? I still cannot figure out on this.
Additional: Before i INSERT INTO...ON DUPLICATE KEY UPDATE to 'Home' table. I LOAD DATA INFILE to a temp table, 'Home_Temp' consist field (home_id, name, and count). Then i INSERT INTO to 'Home' table consist field (home_id, name, count and status). The 'Home_Temp' table is created when user click button for import file and drop after a insert process to 'Home' table.
Home_Temp, update it based on the latest values for eachhome_idinHome. Then write those updated values toHomeidcolumn or similar so then the "latest" value for a givenhome_idwould be the one with the highestidvalue.Home_Temp?. BecauseHome_Temptable is created when user click button for import file and drop after a insert process toHometable. @NickLOAD DATA INFILE INTO TABLE Home_Temp; UPDATE Home_Temp SET status = 0, count = count + (SELECT count FROM Home WHERE home.home_id = Home_Temp.id AND id = (SELECT MAX(id) FROM Home h2 WHERE h2.home_id = Home.home_id)); INSERT INTO Home SELECT * FROM Home_Temp;