I am trying to do some automatic function on MySql.
I need that all arrows of a column named vipdays decrease by 1 on a daily basis.
I mean.. today the values of all arrows of the column vipdays = 30
Tomorrow all values = 29.. next day 28... and I need that this function
be automatic, that works without manually removing 1 day.
I have made some research and found some MySql Scheduler that automate
some functions but can't make it to work.
Any Ideas?
You should create EVENT that is executed once a day and enable scheduler (it is disabled by default).
It should work.
However, do you really want to do this?
You can store expire date in database instead of the number (ex: "DATE_ADD(CURDATE(), INVERVAL 30 DAYS)") and then compare it with CURDATE() to check if it is expired.
Related
I am setting up a MySQL (Maria) where I am going to upload demo data for an application. In the demo date there will be many different dates from the last 6 months.
What I need to do is to keep all dates the same number of days from today all the time. So a date that is x number of days earlier or later than today keep that interval constant.
The demo data will be used in a PHP application so it is maybe an option to run PHP code to change the dates every day, or is it best done any other way?
I have been trying to just add +1, but it does not work on dates.
Hello to everyone and thanks for the help.
I have two different scripts, and each one needs to run automatically every 14 days at a specific hour of the day. (Yes, it's payroll-related.) I haven't seen a trigger that will do this, but then again, I may have missed something...
Anybody care to weigh in about how this can be done?
You will need to programmatically set up triggers and initialize it the first time. Create a new date with
var runTime = new Date()
and you can then simply offset the date with
runTime.setDate(runTime.getDate() + 14)
and set the exact runtime, for example
runTime.setHours(12)
runTime.setMinutes(0)
to run at 12:00 (no point in going deeper, because it will be with a 15 minute error margin as it can run as late as 12:15). Read here on how to set up the trigger.
This will simply have to be done every time you run the script. You clear the old trigger and have the function create a new one for next time. Since you work with a date object, you can also highly customize this to run for example every 10th of the month, or every second Thursday etc. Really depends on how well you know how to manipulate the date objects.
I'm creating a database table where rows need to be removed after a set time. That time is defined in minutes by the valid_time cell in that row. I found this answer though I am not sure how I can implement what I need to into it.
Is someone able to tell me how I can implement this time (as minutes) into the event in the previous answer or if it's not possible, another way to do so. Thanks.
Clarification, I have two columns in the table. One is created which is a TIMESTAMP of when the row is created, and the second is valid_time, an integer in minutes of how long the row is valid for.
DELETE FROM table WHERE created < DATE_SUB(NOW(), INTERVAL `valid_time` MINUTE)
You can try to use the MySQL event scheduler and attach a DELETE query to it. That DELETE will be a simple query that will delete all records where current_time is greater that the valid_time/valid_until fields.
You can configure the scheduler to run in a minute/hourly/daily/... basis as you wish to erase the registers.
Check here and here for more information. M0rtiis offered the query example.
I am trying to write a query to extract records updated in the last 2 hours in SQL Server 2008.
Could anyone help me write this?
select * from table where table.date1>=dateadd(hh,-2,getdate())
dateadd() function lets you subtract hours from getdate() letting you choose records updated past 2 hours
First, you have to design the table so you have a field where the time of the last change will be stored
Then, whenever you update a row, update the value in the 'last update' field. After that, you can use a script such as suggested by Vijaykumar
The downside of this method is that when a single record was changed more than once in the specified time period, you will be notified only about the time of the last update.
Another solution for tracking the updates is to read the database online transaction log file, but you'll need a third party tool for that
I'm working on an app that is partly an employee time clock. It's not too complex but I want to make sure I head in the right direction the first time. I currently have this table structure:
id - int
employee_id - int (fk)
timestamp - mysql timestamp
event_code - int (1 for clock in, 0 for clock out)
I've got everything working where if their last event was a "clock in" they only see the "clock out" button and visa-versa.
My problem is that we will need to run a report that shows how many hours an employee has worked in a month and also total hours during the current fiscal year (Since June 1 of the current year).
Seems like I could store clock in and outs in the same record and maybe even calculate minutes worked between the two events and store that in a column called "worked". Then I would just need to get the sum of all that column for that employee to know how much time total.
Should I keep the structure I have, move to all on one row per pair of clock in and out events, or is there a better way that I'm totally missing?
I know human error is also a big issue for time clocks since people often forget to clock in or out and I'm not sure which structure can handle that easier.
Is MySQL Timestamp a good option or should I use UNIX Timestamp?
Thanks for any advise/direction.
Rich
I would go with two tables:
One table should be simple log of what events occurred, like your existing design.
The second table contains the calculated working hours. There are columns for the logged in and logged out times and perhaps also a third column with the time difference between them precalculated.
The point is that the calculation of how many hours an employee has worked is complicated, as you mention. Employees may complain that they worked longer hours than your program reports. In this case you want to have access to the original log of all events with no information loss so that you can see and debug exactly what happened. But this raw format is slow and difficult to work with in SQL so for reporting purposes you also want the second table so that you can quickly generate reports with weekly, monthly or yearly sums.
Is MySQL Timestamp a good option or should I use UNIX Timestamp?
Timestamp is good because there are lots of MySQL functions that work well with timestamp. You might also want to consider using datetime which is very similar to timestamp.
Related
Should I use field 'datetime' or 'timestamp'?