Go Back   Forum Care Forums > Development Reference Area > MySQL Discussion

Reply
 
LinkBack Thread Tools Display Modes
How to implement a counter?
Old
  (#1)
Flarky
Guest
 
Posts: n/a
Default How to implement a counter? - 06-04-2007, 07:47 AM

Hi,
I want to implement a hit counter using SQL.
I was going to just have one per day in the year.
I would like it if there were an atomic incrementer
in SQL so that if a numeric column let's say
had a value of 5, I could just automatically
increment it to 6 with one SQL command,
without worrying about doing a select, delete,
and insert, which could be interrupted.
Is there a command for this, however?
Thanks.

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: How to implement a counter?
Old
  (#2)
Brian Wakem
Guest
 
Posts: n/a
Default Re: How to implement a counter? - 06-04-2007, 07:47 AM

Flarky wrote:

> Hi,
> I want to implement a hit counter using SQL.
> I was going to just have one per day in the year.
> I would like it if there were an atomic incrementer
> in SQL so that if a numeric column let's say
> had a value of 5, I could just automatically
> increment it to 6 with one SQL command,
> without worrying about doing a select, delete,
> and insert, which could be interrupted.
> Is there a command for this, however?
> Thanks.



UPDATE table SET col = col+1 WHERE ...;


--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: How to implement a counter?
Old
  (#3)
Dikkie Dik
Guest
 
Posts: n/a
Default Re: How to implement a counter? - 06-04-2007, 07:47 AM

> UPDATE table SET col = col+1 WHERE ...;

Depending on the version used (>4.1, I think), you can even
Combine an insert with an update:

INSERT INTO SomeTable(CounterName, Hits) VALUES('JustAName', 1) ON
DUPLICATE KEY UPDATE Hits = Hits + 1;

You'd need a uniqueness constraint on the CounterName column for this to
work.

Best regards
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top