worth2talk Posted April 17, 2007 Share Posted April 17, 2007 Hi Gurus, I am a newbie for MYSQL. The i want to know is as described below: I have created a table say 'temperature' as below. id | currentTemp | MinTemp | MaxTemp | Time ---|--------------|-----------|----------|---------- 1 | 25 | 5 | 32 | 10:10 am and so on... Now suppose i want to increment 'currentTemp' field data by 1 at every 1 hour, then how to do it ? One simple way is to read this row and then add 1 to 'currentTemp' field value and update the 'currentTemp' field. Is there another way to achieve the same ?? Quick answer will be appreciated ..!! Quote Link to comment https://forums.phpfreaks.com/topic/47426-help-how-to-increment-a-data/ Share on other sites More sharing options...
Wildbug Posted April 17, 2007 Share Posted April 17, 2007 You can use a user-defined variable: SET @a:=0;UPDATE temperature SET currentTemp=currentTemp+(@a:=@a+1); Quote Link to comment https://forums.phpfreaks.com/topic/47426-help-how-to-increment-a-data/#findComment-231448 Share on other sites More sharing options...
artacus Posted April 17, 2007 Share Posted April 17, 2007 I'm assuming that you want to add a new row for each reading. What you want is an auto-increment field for id. http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html Quote Link to comment https://forums.phpfreaks.com/topic/47426-help-how-to-increment-a-data/#findComment-231452 Share on other sites More sharing options...
bubblegum.anarchy Posted April 18, 2007 Share Posted April 18, 2007 a cronjob that runs the following query every hour INSERT INTO temperature SELECT currentTemp + 1, MinTemp, MaxTemp, Time FROM temperature ORDER BY id DESC LIMIT 1; Quote Link to comment https://forums.phpfreaks.com/topic/47426-help-how-to-increment-a-data/#findComment-231784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.