I’m a PHP noob.
I have a database that I will update weekly with a CSV. So far I’ve managed to upload the file to the server, open the file with PHP, and insert the data in my table.
Now I want to return the unique records that were added (on screen or in a file). How do I do this?
What makes them unique? We need a little more information about your schema.
Anyway, you can easily add a
date_addedcolumn that defaults toCURRENT_TIMESTAMP. Then you could run a query that sorts thedate_addeddescending to find out what was added.With regard to only pulling unique results.. well, you shouldn’t be re-inserting a unique row, so if you correctly avoid doing that, then the
date_addedwill only reflect truly new entries.Here is a code sample:
First, add the
date_addedcolumn to your table:The
DEFAULT CURRENT_TIMESTAMPwill take care of using the current date automatically, so you do not have to change anything about your existingINSERTcode.With this column in place, you can now easily check which records were added for a specific date:
The
DISTINCTwill make sure you only see unique rows that were added on that date.Here’s another handy query, which will pull “today’s records”:
The only thing you need to remember is that existing records will have a date of 0, since the column did not exist at the time of their inserts.