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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
> 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 |