Do it the other way around.
If insert fails, do the update.
Udate may fail, and before you insert, another tread could do the
insert.
Now your insert would fail, and your data would be lost.



Pål Wester wrote:
> 
> Why not do an update regardless if the record exist or not,
> then insert it if you get an error?
> 
> -----Opprinnelig melding-----
> Fra: Artem Koutchine [mailto:[EMAIL PROTECTED]]
> Sendt: 26. januar 2001 19:02
> Til: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Emne: Re: If exists UPDATE else INSERT
> 
> I've seen something about this in the manual. At the moment
> the problem was how to call such an SQL command (like
> REPLACE for DELETE/INSERT). I wonder if it was
> implemented in 3.23.32 or is it only in 4.x?
> 
> ----- Original Message -----
> From: "Jan Willamowius" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Friday, January 26, 2001 7:34 PM
> Subject: If exists UPDATE else INSERT
> 
> > I'm have a lot of queries where i have to check if a record exists
> > and if it does, increment a counter in it (most of the time). If it
> doesn't
> > exist I insert a new record with counter=1 (this case is rare).
> >
> > Right now I do a select form Perl and then do one or the other.
> > To speed things up I could always do an INSERT IGNORE and always an
> UPDATE.
> > This would save one statement on the INSERT case, but that is rare
> anyway.
> >
> > I'd really like to do this in one (fast) statement, since this is a
> > real time application with literally milions of UPDATEs that come in
> > this way every day.
> >
> > Would it be possible to write a UDF to do this ?
> >
> > Cheers,
> > Jan
> > --
>

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to