Jump to content

Get Auto Inc ID on Duplicate


stevil

Recommended Posts

I'm working on a fairly large project at the moment and need to limit queries to a minimum. I'm looking for a way to insert a new col, but if the col has a duplicate in it then return the auto inc/key field for that table.

 

	//Gets Date ID or creates one if need be
mysql_query("INSERT INTO dated(date_date) VALUES('".date("Y-m-d")."') ON DUPLICATE KEY UPDATE date_id = date_id;");
$DateID = mysql_insert_id()-(mysql_affected_rows()>1?mysql_affected_rows:0);

 

This worked until the table was altered and the insert_id was out.

 

	$sql="SELECT date_id FROM dated WHERE date_date='".date("Y-m-d")."'";
if($DateCHK	=mysql_fetch_array(mysql_query($sql))){
	$DateID	=$DateCHK['date_id'];
}else{
	$sql	="INSERT INTO dated(date_date) VALUES('".date("Y-m-d")."')";
	$DateCHK=mysql_query($sql);
	$DateID	=mysql_insert_id();
}

 

This also worked, but I started getting errors because we were getting multiple dates showing up in the database. So basically two or more people were checking if the date exisited at midnight at the same time... I know it sounds unlikely, but it's happen on several occassions while in testing.

 

I also find the above method is slow, and runs more queries than needed.

 

I've also looked into doing a REPLACE, this is REALLY slow though as it has to delete the record each time someone uses it. As the query is going to be run well over a million times a day, speed is of high importance.

 

Can anyone see a possible way to do this which will be fast an accurate?

Link to comment
Share on other sites

Why not use the first part, but in two parts?

 

Not sure if I get exactly what you mean sorry? Doing the ON DUPLICATE KEY UPDATE method in 2 parts?

 

I've come up with a solution that would be better as far as errors go. I can try and insert the row first, if the keys are duplicate it'll reject it meaning 0 rows are affected meaning I knwo the row is already created. I assume this is pretty slow though aswell.

 

Is there a way maybe to do a SELECT statement with an IF condition on it that does an INSERT ? I highly doubt that it's possible =( but someone might know something along those lines.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.