List:General Discussion« Previous MessageNext Message »
From:Jim McAtee Date:December 3 2004 3:56am
Subject:More efficient way?
View as plain text  
I have an application which keeps a table of daily event counters related 
to other records in a databse.  Since the trackingrecords are kept on a 
daily basis new records are created each day for items being referenced.

In pseudo-code:

// Check for the existance of daily tracking record
SELECT dailycounterid
FROM dailycounters
WHERE trackingdate = <today>
  AND thingid = <somerecordid>

if <query.recordwasfound>

  // If it exists, increment counter
  UPDATE dailycounters
  SET count = count + 1
  WHERE dailycounterid = <query.dailycounterid>


  // Otherwise add new record with count of 1
  INSERT INTO dailycounters
    (trackingdate, thingid, count)
    (<today>, <somerecordid>, 1)


Is there any way to do this with a single MySQL query instead of a check 
followed by either an insert or an update?  The MySQL version is 3.2x 
using MyISAM tables.

More efficient way?Jim McAtee3 Dec
  • Re: More efficient way?Eric Bergen4 Dec
  • Re: More efficient way?Jim McAtee5 Dec
    • Re: More efficient way?Michael Stassen5 Dec