Torgil Zechel writes:

> table stats
>       count           int unsigned not null default 0
>       date            timestamp (using only year/month part)
> 
> I update this table with:
> 
> UPDATE stats SET count=count+1 WHERE date=XXX
> 
> BUT, if the date is not there, no counter will be updated so I must first
> check this and insert a record if date is not found. Since there is more
> than one thread that can write to this table I must use a lock:

> Can this be specified as one statement, so that I dont have to use table
> locks??

You could to the UPDATE first, and if it updates 0 rows
do an INSERT IGNORE, and if that too changes 0 rows another
thread has inserted a row with that date in the meantime
and you just have to do the UPDATE again.

UPDATE ...
if(changed 0 rows)
{
  INSERT IGNORE ...
  if(changed 0 rows) UPDATE ...
}

Of course you could skip the first UPDATE and try the
INSERT immediately, but this will probably be less efficient
since most of the time the first UPDATE would be all that
is needed.

//C

-- 
 Carl Troein - Círdan / Istari-PixelMagic - UIN 16353280
 [EMAIL PROTECTED] | http://pixelmagic.dyndns.org/~cirdan/
 Amiga user since '89, and damned proud of it too.


---------------------------------------------------------------------
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