I would like to set up MySQL so that it can subtract one from all values in a row every minute. Once that value equals 0, I would like MySQL to delete that row, and execute a query which adds a row to another table. Before I ask how this can be done, I first would like to know if such a thing is even possible. I have looked around on Google using the search term "automatic run sql query", and unless I am incorrectly understanding what I have read, it is possible, but how can I do something like what I want? What I want would need an if statement to work, but does MySQL even have such a thing as if statements?
You can use the recurrent option in the MySQL event scheduler. You an set it to run every min and then have your logic in it.
http://dev.mysql.com/doc/refman/5.1/en/events-overview.html
Using recurrent event will help you to accomplish this. Create an event to execute every minute.
CREATE EVENT subtractRow
ON SCHEDULE
EVERY 1 MINUTE
STARTS NOW
ON COMPLETION PRESERVE
COMMENT 'Row Subtraction'
DO
-- WRITE QUERY TO SUBTRACT ROW VALUE HERE
Related
I'd like to know if mysql(or mariadb) offers a function for the expiration that a row can be removed automatically in the DB without using any extra scheduler program nor using any SQL like DELETE.
This should happen or define when you create a table so that once INSERT occurs it starts to manage it.
There are many related questions here:
MySQL how to make value expire?
Remove Mysql row after specified time
MySQL give rows a lifetime
However, I couldn't find the answer. I am not curious about using WHERE nor DELETE.
Is it even possible?
Yes for same you can create an event by this way
CREATE EVENT lifetime ON SCHEDULE
EVERY 1 DAY STARTS '14:05:44' ENDS '14:05:46'
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
// put your delete query here with where clause by calculate your exp date
END
am trying to generate an event that delete a table in a DB
my idea is to make that to run every (saying hypotetically) 10 min for ever ( no end date)
but I get an error as following:
The starts and ends parameters of the create event command are timestamps. Currently the upper limit of timestamp in mysql is 2038-01-19 03:14:07.999999'. You can't specify any ends after this point.
If you want the event to run "forever", then do not specify any ends (although I would be surprised if your application would run unchanged beyond 2038...)
I have an SQL database with over 800 entries and 40 values for each entry. I would like to add a column that increases every time any of that row's values change. It would be like a version number for each individual row so I know which rows have edited data.
I know little to nothing about sql coding, I just use phpmyadmin to hold my data. Is something like this possible without adding some sort of function, and if not, how would I go about implementing something like this. Any input would be appreciated.
You can use triggers to do this job. Run the following query in phpmyadmin after editing the table name and column name for count
CREATE TRIGGER incr_on_update BEFORE UPDATE ON yourtablename
FOR EACH ROW SET NEW.count =OLD.count+1;
For a smart metering application i would like to aggregate counter data every hour.
For this a query like 'select sum(value) as GasCounter from data where channel_id=8' gives the actual counter value.
For another display task i need this as data of channel 25 every hour in the same table 'data'. Therefor i have to add a dataset and put a valid unix-timestamp in the field 'timestamp' and the sum value in the field 'value'.
All this must be done every hour.
Can Mysql do all that just by using a routine?
What is the right routine sql? How do i create the routine? How is the trigger done so that the routine is executed everx hour?
I need your help on this!
Thank you!
Create a procedure that process what you need to make your middleware happy and then use an event scheduler to call your procedure every one hour.
I'm completely clueless as to do a cron job/mysql event task for some things in my database to be updated automatically every 24 hours.
Basically here's the 2 queries I need to be ran every 24 hours as soon as users_stats DailyRespectPoints hits '0'.
UPDATE user_stats SET DailyPetRespectPoints = '3' and
UPDATE user_stats SET DailyRespectPoints = '3'
Those are what needs to be ran so that when someone uses all 3 of their DailyRespectPoints, it refills back to 3 in 24 hours.
Thanks!
you could create trigger , which will be executed automatically when DailyRespectPoints hits '0'.
you can read more about the triggers here http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
or you can write more details about your database/query-s and we can help you write the trigger
First write a script that will perform the work you need done. Based on your question, you will check some table value, then execute your two UPDATE steps.
After your script is tested and performs correctly, you can schedule it to run every day at some point in time using cron (probably crontab).