MySQL - How to decrement int value every 24 hours? - php

I am working a system in PHP. In my MySQL have a table borrow. Inside borrow is borrow_status a varchar and borrow_remain which is an int value. When the user click the submit button. The query will run something like
INSERT INTO `borrow`( `borrow_status`,`borrow_remain`) VALUES ('Borrowing',3)
How can I decrement the borrow_remain value per 24 hours then UPDATE the borrow_status to Finish when it reaches zero?
EDIT: I also have a book table, that have a book_quantity, my only concern is that the book_quantity must increment if the borrow_status is updated to Finish.

You can implement the above scenario following the three steps given below :
1) First you have to enable Mysql event scheduler.
SET GLOBAL event_scheduler = 1;
2) Create a procedure in mysql that will do the borrow status update related functionalities.
delimiter //
CREATE PROCEDURE UBS()
BEGIN
UPDATE borrow set borrow_remain = borrow_remain - 1
WHERE borrow_status='borrowing';
UPDATE borrow SET borrow_status = 'finished'
WHERE borrow_remain=0;
END //
delimiter ;
3) Create an event which will be scheduled in 24 hours interval.
CREATE
EVENT UBSEvent
ON SCHEDULE EVERY 24 HOUR STARTS '2015-03-04 00:00:00'
ON COMPLETION PRESERVE
ENABLE
DO
CALL UBS();
Note :
UBS is the procedure name.[ UBS = Update Borrow Status (just a
relevant abbreviation ]
The event will start from tomorrow (4 March 2015) at 12:00 AM
Have a nice day!
EDIT: To increment book_count in book table whenever a borrow_status in borrow table gets updated to 'finished' you need a trigger.
Suppose you have a book table where the count of the book is denoted by book_count; Then the following trigger will do what you want.
DELIMITER $$
CREATE TRIGGER IncBookCount AFTER UPDATE ON borrow
FOR EACH ROW
BEGIN
IF NEW.borrow_status = 'finished' THEN
UPDATE book SET book_count = book_count+1;
END IF;
END $$
DELIMITER;
SUGGESTION : You need to improve your database design. Otherwise you might get more troubles in designing a complex architecture in future. Stay well!

Have you look at Events in mySQL? You can create an event and set it to run on a particular schedule. That might be the ticket for your problem!

You need to have mysql version 5.5 to use event. May be your desired event looks like this:
CREATE EVENT update_borrow
ON SCHEDULE EVERY 24 HOUR
STARTS CURRENT_TIMESTAMP
DO
BEGIN
UPDATE borrow SET borrow_remain = borrow_remain - 1 WHERE borrow_remain > 0;
UPDATE borrow SET borrow_status = 'FINISH' WHERE borrow_remain = 0;
END

You could also set a cronjob that executes a piece of code to decrement "borrow_remain" and updates "borrow_status" accordingly. If your hosting has cPanel it's pretty easy: https://documentation.cpanel.net/display/ALD/Cron+Jobs

Related

How to write an event scheduler that run every midnight ? And set a value to 0

I have written an Event Scheduler in MySQL I don't why it's not working.
I would like to set the reserved = 0. the event should run every midnight at 00:01:00 european time. when the expiration date (exp_date) is less then current date time here is my script.
CREATE EVENT IF NOT EXISTS disable_reserved_value_of_prospect
ON SCHEDULE
EVERY 1 DAY
STARTS '2022-03-30 00:01:00'
ON COMPLETION PRESERVE
ENABLE
DO
UPDATE prospect
SET reserved = 0
WHERE exp_date < CURRENT_TIMESTAMP;
But my script doesn't work I don't know why ?
Can we use any if condition inside event script?
Thanks in advance
ON COMPLETION PRESERVE is for one-time event only,so you don't need it in your recurring event. Try modifying the code like below:
delimiter //
drop event if exists disable_reserved_value_of_prospect //
CREATE EVENT disable_reserved_value_of_prospect
ON SCHEDULE
EVERY 1 DAY
STARTS '2022-03-30 00:01:00' DO
BEGIN
UPDATE prospect
SET reserved = 0
WHERE exp_date < CURRENT_TIMESTAMP;
END //
To verify, we can reduce the 1st kick-in time by using EVERY 1 minute STARTS now(). If nothing happens, there is probably something wrong with the UPDATE statement. Substitute it with a TEST statement to insert a line into a test table or something.

Database which can remove records automatically by checking date column

Suppose there is a database which contains a table that has date and events column, there are total 10 records now I want that the records which are older then the current date should automatically be deleted and the present date records should be present in the database so as the future records should also be present, does anyone know something like this or can help me how to achieve this. any references would be helpful. I am open to php, phpmyadmin, sql. Any help would be appreciated.
First it's too broad. You can consider using MySQL Event Scheduler for this purpose. Which you can set to run on every 24 hours and delete all records less than NOW()
Activate event support :
SET GLOBAL event_scheduler = ON;
Add sheduler :
DELIMITER |
DROP EVENT IF EXISTS `clear_old_rows`|
CREATE EVENT `clear_old_rows`
ON SCHEDULE
EVERY 1 HOUR STARTS CURRENT_TIMESTAMP
ON COMPLETION PRESERVE
ENABLE
COMMENT 'clear all rows < CURRENT_TIMESTAMP'
DO
BEGIN
DELETE FROM tablename WHERE date < CURRENT_TIMESTAMP ;
END |
DELIMITER ;

Adding +1 to a mysql value every day

hello I have a table called 'members' for my website/php code and I dont know how to increase a certain value by 1 everytime a new day starts (or eventually everyday the user logs in the first time).
Can anyone help me?
-thanks.
You can do this in either where you process the user login, or in a file that's scheduled with a cron job:
For everyone:
UPDATE members SET field = field + 1;
For one person
UPDATE members SET field = field + 1 WHERE user_id = 42
If anyone finds this through searching or what not you could do something like the following:
SET GLOBAL event_scheduler = ON; -- enable event scheduler.
SELECT ##event_scheduler; -- check whether event scheduler is ON/OFF
CREATE EVENT e_store_ts -- create your event
ON SCHEDULE
EVERY 24 HOURS -- run every 24 hours
DO
UPDATE myschema.youtable set mycolumn='1'
Source: https://dba.stackexchange.com/questions/43293/updating-a-column-after-expiration-of-specific-time-in-mysql

How to increment mysql data after a fixed time slot automatically?

For example, I am creating a student management system and I want to increase the student standard after every year. If student is admitted in 1st standard then next year he would be in 2nd after that 3rd and so on.. Please help!
you can use MySQL EVENT like
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 YEAR
DO
UPDATE `student` SET `standard` = `standard` + 1
WHERE some_ID_Column = value;
Set up a cron job that runs nightly and run a stored procedure that checks if the students have reached the condition for the next standard and if they have update the records.

How to INSERT days to keep a MYSQL calendar table current?

I have a calendar table in my MYSQL database with one field named datefield (type DATE).
Previously I inserted dates from 2008-01-01 to 2010-01-01 but now I want to add every day until the current date. Unfortunately, I'm completely forgotten how I did this.
I'm also wondering if there is any way to automate the process of keeping this table current.
I'm using PHP so I guess I could do a new INSERT every day? Or is there some way to do this directly in MYSQL?
thanks,
tim
Here's a quick hack:
drop table if exists dates;
create table dates(datefield date);
drop procedure if exists insertDates;
delimiter $$
create procedure insertDates(start_date date)
begin
set #days = 0;
set #end_date = str_to_date('1970-01-01', '%Y-%m-%d');
while (#end_date < curdate()) do
insert into dates
select
date_add(start_date, interval #days day)
from (select #days:=#days+1) r
;
set #end_date:=(select max(datefield) from dates);
end while
;
end $$
delimiter ;
call insertDates('2012-06-06');
select * from dates;
For the daily insert you can try a cron job or MySQL offers "EVENTS", something like:
CREATE EVENT yourDB_Schema.insertDateEachDay
ON SCHEDULE EVERY 1 DAY
DO
INSERT INTO yourTable (yourDateColumn) VALUES (CURDATE());
Read more about it here:
MySQL Create Event manual entry
An event is associated with a schema. If no schema is indicated as
part of event_name, the default (current) schema is assumed.
_
Not specifying STARTS is the same as using STARTS
CURRENT_TIMESTAMP—that is, the action specified for the event begins
repeating immediately upon creation of the event.
I think the easiest way to do what you describe is set up a daily recurring task to run a php script, such as a cron job. There is also a web based provider that will call a script from your webserver on a cheap, recurring schedule. http://webcron.org

Categories