Bluehost.com Web Hosting $6.95

How to implement a counter?

This is a discussion on How to implement a counter? within the MySQL Database forums, part of the Database Forums category; Hi, I want to implement a hit counter using SQL. I was going to just have one per day in ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-13-2007
Flarky
 
Posts: n/a
Default How to implement a counter?

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.

Reply With Quote
  #2 (permalink)  
Old 01-13-2007
Brian Wakem
 
Posts: n/a
Default Re: How to implement a counter?

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
Reply With Quote
  #3 (permalink)  
Old 01-13-2007
Dikkie Dik
 
Posts: n/a
Default Re: How to implement a counter?

> 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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:02 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0