I am writing a MySQL Database and I wish to do the title within the MySQL database itself via Events if possible. I am updating the database via a Windows Service written in VB.net and have no desire to do the below via a separate 'server control' type program.
Basically, I have written the below in to the Database, but it doesn't quite do what I want:
Create Event MachineOffline
On
SCHEDULE Every 2 Minute
Do
UPDATE maindb.monitortable SET Online='1';
I would like it to say something like the below:
Create Event MachineOffline
On
SCHEDULE Every 2 Minute
Do
IF **RowX updated < 2 minutes ago** then
UPDATE Database.Table SET Online='0' where RowX(ID);
I have no idea how to achieve this hence my question to everyone. I have no desire to create
If you give your Database.Table an extra column, lets say, last_updated timestamp, you can then update it like so:
update Database.Table
set online=0, last_updated = now()
where last_updated <= now() - interval 2 minute
and online=1
Related
I'm wondering if there is any way to schedule an update where I update a tables data on expiration of the old one.
E.g. John Smith has an active service at Company X, but wants to upgrade the service. However, due to restrictions to his current agreement the new service doesn't take effect until X days.
Is there any way to store the new data and update it at the end of a month in MySQL? If yes, would this require me to have another table with the new order data stored?
Often this requirement for changing things at future times is handled by placing start_date and end_date columns in your services table.
Then you can find presently active service rows with
SELECT user_id, whatever, whatelse
FROM services
WHERE (start_date IS NULL OR start_date <= NOW())
AND (end_date IS NULL OR end_date > NOW());
You can, if you wish, create a view called active_services automatically filtering the services table for currently active services.
CREATE VIEW active_services AS
SELECT *
FROM services
WHERE (start_date IS NULL OR start_date <= NOW())
AND (end_date IS NULL OR end_date > NOW());
Note -- in this design end_date contains not the last moment the service is active but the first moment it becomes inactive. If end_date is null the service continues to be active.
To change a service or user_id at the beginning of next month you do these two operations:
UPDATE service
SET end_date = LAST_DAY(NOW()) + INTERVAL 1 DAY
WHERE user_id = <<user id you wish to change >>
INSERT INTO service (start_date, user_id, whatever, whatelse)
VALUES (LAST_DAY(NOW()) + INTERVAL 1 DAY,
<<user id you wish to change >>,
<<whatever>>,
<<whatelse>>;
Then, when next month arrives the active_services view returns the new service. This is much more robust than relying on a precisely timed monthly job. If you do a monthly job, it can run anytime. It simply cleans up expired services.
You can use MySQL events to run particular jobs at appointed times. (But some shared MySQL systems don't let you use events.)
Interestingly mysql has event schedulers have a look at the documentation.
below is an example of a minimal CREATE EVENT statement:
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
The previous statement creates an event named myevent. This event executes once—one hour following its creation—by running an SQL statement that increments the value of the myschema.mytable table's mycol column by 1.
If yes, would this require me to have another table with the new order data stored
I am not sure if you need another table or so...it purely depends on how you want it to be one
Lets say I have a Table tbl_Room with a column taken (boolean) and a Customer wants to rent this room for a short period.
Now, can I tell mysql to change the value of taken automatically depending on the timestamp, e. g. if the rent time/period is over, the value of taken should set automatically to false.
Or do I need to update my database with CRON or some other script that runs on the server periodically?
Please use mysql event to manage it.
CREATE EVENT [IF NOT EXIST] event_name
ON SCHEDULE schedule
DO
event_body
Reference
Under event_body you can write select statement to check period and then update table if period is over.
The best way to handle this sort of time-based request is counterintuitive.
Don't try to update the table at a specific time. Instead, include a timestamp column called something like lease_expires_at.
When you rent a room, update the row to set the value of lease_expires_at to the time at which the rental period expires. For example, if you rent a room for 30 minutes, starting now, do this.
UPDATE room
SET lease_expires_at = NOW() + INTERVAL 30 MINUTE
WHERE room_number = whatever
If you want to know whether a room is presently (NOW()) taken, do this:
SELECT room_number,
CASE WHEN lease_expires_at IS NULL THEN 0
WHEN lease_expires_at <= NOW() THEN 0
ELSE 1 END taken
FROM room
WHERE room = whatever
If you want to know whether a room will be available one hour from now (NOW() + INTERVAL 60 MINUTE), do this:
SELECT room_number,
CASE WHEN lease_expires_at IS NULL THEN 0
WHEN lease_expires_at <= NOW() + INTERVAL 60 MINUTE THEN 0
ELSE 1 END taken
FROM room
WHERE room = whatever
Then, once in a while, but not in any time-critical way, you can clean things up using a query like this
UPDATE room SET lease_expires = NULL WHERE lease_expires <= NOW()
You can use an event, or an overnight cronjob, or whatever you wish, to do this cleanup. The integrity of your application doesn't depend on exactly when this job runs.
The advantage of this should be clear: If you rely on some regularly running process to set an taken column value, and that process doesn't run or runs late, you get bad results. When you rely on the time, you get accurate results.
There's a small boundary-condition detail in this design. By using <= in my queries, I'm choosing to have the lease_expires_at timestamp represent the very first moment at which the room is available for another lease, not the last moment of the present lease. That's a handy choice, because if you put something like 2017-11-2017 11:00:00 into lease_expires_at, and somebody says "is the room available at 11:00?" you want to be able easily to say "yes." The guy who rented it at 10:30 gets it until the moment before 11:00.
you can use jquery time picker....after u can create a if loop in which JavaScript time function will check current time...to its original time...if condition is satisfied...we can change the mysql taken function
I have date as DB column
Can someone advise on a command in phpmyadmin which will unpublish all records more than 6 month old
For Instance -
v_id : unique record value given to every record
Adddate : Date field - values stores as 2015-11-23 09:39:28 (Year-Month-Date Time)
v_status : 1 means published and 0 means unpublished
Table name xyz_used
What command can i run in my phpadmin so that it can unpublish i.e make V_id record status i.e. v_status as 0 having adddate as before 2015-05-31 in table xyz_used
Pls am not aware of command at all - hence asking question for help
thanks
first of all phpmyadmin is a tool or webapp to access mysql databases. you can say it is a client utility.
Second
To update a table you will need to write Update Sql Query where you can put the date and can set the status.
query would be like
Updaate <tbl> set v_status = 0 where datefield < date_sub(now(), interval 6 month);
I have been developing ban system with temporary banning system and I'm looking if there's anyway to update a row to specific value using a timer within MySQL. For instance,
I need my field banned to be set to 0 where the rowid is X.
UPDATE mytable SET banned=0 WHERE rowid=X
- But this query has to be done within X minutes or seconds specified, like if we could delay it.
Banning a user temporarily for 60 seconds, query sent at that time:
"UPDATE mytable SET banned=1, unbanon=NOW()+60 WHERE rowid=X".
Could I possibly get MySQL to automatically set banned variable to 0 after 60 seconds has passed?
Thank you.
Set a cron job to run a query every minute that will do something like :
UPDATE mytable SET banned=0 WHERE unbanon <= NOW() AND banned=1
I have a table that stores some sensitive information but I would like that information to change to NULL when it exceeds 24 hours. How can I do that? I have a column named "last_updated" and stores value like this "2014-02-26 16:25:58".
How can I compare the last_updated value with the current time and if it exceeds 24 hours, the other field will change to "NULL".
Should I put something like UPDATE table SET info=NULL WHERE last_updated > 24hour? I don't know how to compare the last_updated when its 24 hours later.
or is there a function inside MySQL to check automatically without running the query using phpmyadmin?
You can do this with a scheduled job that resets the data. Of course, the time span would be 24-48 hours to the change, if you run the job only once per day.
There is another option. That is to do all the data access via views. Then the view could say:
create view v_table as
select (case when last_updated > now() - interval 1 day then col1 end) as col1,
. . .
from table;
Then, you can then update the data at your leisure -- if you still find that necessary. Access to the data won't be dependent on a job and job scheduler. If all accesses to the data are through the view, then after 24 hours, the data will appear as NULL.