View Full Version : php/sql handling of duplicate records

06-26-2011, 10:47 PM

I have a website that accepts account signups. I insert filler records that some users can "claim", meaning they will see their account listed on the site and want to update/use the profile.

So, if on signup I want to check to see if there's an account with matching fields (ie name and employer), would that require a stored procedure, cursor and / or something similar?

Old Pedant
06-26-2011, 11:34 PM
You could do it with a stored procedure, but you don't have to.

You could just do it with an ad hoc query.

Just do a SELECT in the table to see if there is a match on the fields you are interested in.

If not match, then use an INSERT.

If there is a match, do an UPDATE instead.

You can *ALSO* always do an INSERT and then add an ON DUPLICATE clause to the INSERT, but (a) that requires that the fields in question be made into a composite index and (b) the query is more complex. Unless you will be inserting/updating thousands of records per hour, I'd just go with the SELECT followed by either INSERT or UPDATE.