Skip to content
Advertisement

How to insert ignore MySQL rows into another table and update a field when duplicates found?

I’m trying to make a telemarketing campaign management web application. Application user can upload Excel sheets including telephone number + status fields to the application and update the “status” when they are making a call to clients. When an Excel sheet is uploaded, all the data goes to a table called “tp_numb”. Then I created “MySQL insert ignore” to copy data from “tp_numb” to a table called “ok_list”. I need to update the “status” field in the “ok_list” table when duplicates are found. If duplicates are not found, the data should be inserted into the “ok_list” table.

Above code is the code I have been trying to improve with a function to update the “status” field in the “ok_list” table. Because the status of telephone numbers should be the status according to the last uploaded Excel file. How can I update the status when a duplicate found? How can I insert a record when duplicates are not found?

Advertisement

Answer

I think you want on duplicate key update:

Your question supposed that there is only one row for each number. I’m not actually sure which column that is, but you should have a unique constraint/index so it know which row gets updated with the status. I think it is tp_numb_id, which would be;

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