I want to perform a time-based operation in MySQL. If the value in the table is 0 while it is 1, the system will start counting time automatically and the value that is 1 should automatically change to 0 at the end of 30 days.
You can use a view for this. Instead of storing the value, store the timestamp when the timing starts. Then just use logic to calculate how recent the value is:
create view v_mytable as
select t.*,
(value_timestamp < curdate() - interval 30 day) as value_flag
from mytable t;
Related
I have a table ‘comenzi’, each order has a value for in progress, and another for ready and data. Each order when it is created has the values for in progress and ready equal to 0. I want to update the values for in progress and ready with 1 min ,after 20 min, then 30 min after the order has been done, the value from data that has the form 2020-05-25 09:41:00.
I need a way to update these values, a function or an event in database.
I was dealing with something using events just yesterday on a pet project. You could use a similar approach.
This code uses a creation column, which is just a TIMESTAMP column with a default value of CURRENT_TIMESTAMP (because I don't have your data) and a value column (default value 0), which goes from 1 - 2 (you can modify this code to suit your needs).
The SET value uses CASEs to check how long ago the order was created. Then it updates the value accordingly. You can change up the event to update whichever columns / time cases you'll need.
CREATE DEFINER =`root`#`localhost` EVENT `progressEvent`
ON SCHEDULE EVERY 1 MINUTE STARTS '2020-09-01 00:00:00'
ON COMPLETION PRESERVE ENABLE DO UPDATE comenzi
SET `value` = (
CASE WHEN (creation < (CURRENT_TIMESTAMP - INTERVAL 30 MINUTE))
THEN 2
CASE WHEN (creation < (CURRENT_TIMESTAMP - INTERVAL 20 MINUTE))
THEN 1
END)
WHERE value < 2
Edit: Changed it up so that it checks every minute and only 20/ 30 minutes.
In my database, I have a table called 'fine', in that table I have three fields, issue_date, expiry_date and fine_amount. I want the expiry_date to be computed from the issue date. The expiry date should always have 20 days more than the issue_date, So I wrote the query as:
ALTER TABLE fine ADD
expiry_date AS DATE_ADD(CURRENT_DATE,INTERVAL 20 DAY)
But there is a syntax error. I can't seem to find the solution.
Also I want the fine_amount to be 10 * (difference in days between current date and expiry date if current days exceeds expiry date). How do I go about doing that?
You can't implement the fine logic using a computed column because the formula involves the current time which is non deterministic. From the MySQL documentation:
Literals, deterministic built-in functions, and operators are permitted. A function is deterministic if, given the same data in tables, multiple invocations produce the same result, independently of the connected user. Examples of functions that fail this definition: CONNECTION_ID(), CURRENT_USER(), NOW().
So your best bet probably is to just compute values for these columns at the time you actually select. For example:
SELECT issue_date,
DATE_ADD(issue_date, INTERVAL 20 DAY) AS expiry_date,
CASE WHEN NOW() > DATE_ADD(issue_date, INTERVAL 20 DAY)
THEN 10*DATEDIFF(NOW(), DATE_ADD(issue_date, INTERVAL 20 DAY))
ELSE 0 END AS fine_amount
FROM fine
I want to UPDATE my data in scheduled time. My problem is that I cant equal the date that I enter in my database in the current real time date. For example, I have 2015/24/9 19:50:00 in my database, now I want to equal it to the current real time date so that I can update a specific row in the database. If I don't do that, the amount field will just multiply 5 in every row. I want to multiply the amount by 5 in a specific row and time
Code:
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = (the the current real time date);
DATETIME and TIMESTAMP values are, like floating-point values, difficult to compare for numerical equality. In other words, if it happens that NOW() = datetimestamp, it's a lucky accident. This is especially true when processing events: the event actually starts to run shortly after the scheduled time.
So, instead of saying something like this
`DATE` = NOW()
say something like this
`DATE` BETWEEN NOW() - INTERVAL 10 SECOND
AND NOW() + INTERVAL 10 SECOND
Of course, such a narrow time interval makes you critically dependent on the time accuracy of the event scheduler. You'd be better off adding a LAST_UPDATED column of DATETIME type to your table, then doing this update.
UPDATE messagesd
SET amount = amount * 5,
LAST_UPDATED = `DATE`
WHERE `DATE` >= NOW() - INTERVAL 1 MINUTE
AND (`DATE` > LAST_UPDATED OR LAST_UPDATED IS NULL)
That way, every time your event runs you'll update all the rows that are due for update, but haven't yet been updated. This is not dependent on the precise time an event runs. The - INTERVAL 1 MINUTE allows the event to be up to a minute late running and still function correctly.
If you need to schedule another update for the future for a particular row, change the value of the DATE column but don't touch the LAST_UPDATED column.
Are you just looking for CURDATE()?
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount * 5
WHERE DATE = CURDATE();
If you need the current real date time use mysql NOW()
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = NOW();
I need to return all the entries in a MySQL database from the last hour. The database has a column named time that has epoch values in.
This bit of psuedo code is what I want to be able to achieve but am not sure how to do this in MySQL. In another language I'd check to see that the epoch value of time in each row is no more than 3,600 different.
SELECT * FROM dailyltc WHERE `time` <= 1 hour
Not using functions to convert your stored epoch value enables MySQL the use of an index. Because of that I prefer the calculate the limits with UNIX_TIMESTAMP instead of converting the stored value to a DATETIME value.
SELECT
*
FROM
dailyltc
WHERE
`time` > UNIX_TIMESTAMP(NOW() - INTERVAL 1 HOUR);
Should newer values exist then you can simply add the upper bound:
SELECT
*
FROM
dailyltc
WHERE
`time` > UNIX_TIMESTAMP(NOW() - INTERVAL 1 HOUR)
AND
`time` <= UNIX_TIMESTAMP(NOW());
I have a database with several tables. Each of these tables has a column 'created' which contains time-stamps of when that particular row was created in the database.
Now, I want to create a MySQL script that checks once every week if there is data coming into these tables. So, there should be data coming everyday. How do I create a MySQL script that allows me to do this for all the tables in the database?
Note: Remember I want to do this for all the tables in the database with a single script. That's the main thing I want to know.
i use this approach for a table called call, with a column of timestamp type called systemdate:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW());
mysql DATE() statement gets the datepart of a datetime or timestamp field.
Sorry, just noticed that you want to check if atleast there is an entry for each of the days in the previous week.
you can use this query to check the prevous days individually:
yesterday:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW()) - 1;
before yesterday:
SELECT * FROM `call` WHERE DATE(`systemdate`) = DATE(NOW()) - 2;
Or you can check the whole week at once:
SELECT * FROM `call` WHERE DATE(`systemdate`) > DATE_SUB(CURDATE(), INTERVAL 7 DAY) GROUP BY DATE(`systemdate`);
This will return one result for each day, so if you have 7 results you'll know at least an entry was made on each day.
select * from table
where created between subdate(current_date, interval 7 day) and current_date;
Selecting datetimes up to current_date includes everything up to the start of "today" (ie "the previous midnight").