I have a table that keeps the last few weeks worth of data.
The system goes offline around 3am daily for a few min.
I would like to run a cron job on Monday around 3am to pull last week's data.
How would I select last week's data (Monday 00:00:00 through Sunday 23:59:59)?
I realize one way would be to simply schedule the cron for 00:00:00 on Monday but I want to run this when the system is offline so I need to use MySQL to delimit the data.
Thanks in advance.
The difficulty is going to be your last second on Sunday - you often get into rounding errors there. One way to solve this is just to format or cast from DATETIME to DATE...
SELECT *
FROM logfile
WHERE DATE(logdate) BETWEEN DATE_ADD(CURDATE(), -1 INTERVAL day) AND DATE_ADD(CURDATE(), -8 INTERVAL day)
Related
I am doing a cronjob that should check the database for new Users, the cronjob runs every day at lunch.
I want to get an email with all users created between YESTERDAY after Lunch and TODAY Lunch, so the last 24 hrs.
How can I do it?
Like
WHERE userCreationDate WITHIN LAST 24 HRS
?!?
Use the DATE_SUB() and NOW() to specify the date then specify your interval.
where userCreationDate > DATE_SUB(NOW(), INTERVAL 1 DAY)
I tried to do the following query to subtract one day from the given date:
DATE(FROM_UNIXTIME(UNIX_TIMESTAMP('2013-04-01') - 86400))
which returns me 2013-03-30, but it should return 2013-03-31.
If i try to subtract one day of 2013-04-02, i get 2013-04-01 correctly returned.
Is this a date bug in mysql?
It's not a bug - what you have found is the missing hour in daylight saving time: a thing unixtime and your calculation is unaware of since you calculate with seconds and not days.
This is exactly why DBMS have special DATETIME datatypes - to handle all the specialities in timezones, leap years, leap seconds, daylight savings and calendars.
Let the database do the work for you - here is an easier and better way to get what you want:
SELECT DATE('2013-04-01') - INTERVAL 1 DAY
Your code makes the assumption that all days have 24 hours. Yesterday, 31st March, had 23 hours in most European countries. To subtract one day you need something like this:
SELECT NOW() - INTERVAL 1 DAY
Why not use the built in DATE_SUB() function?
SELECT DATE_SUB('2013-04-01', INTERVAL 1 DAY)
EDIT: No, This is NOT a date bug in MySQL.
I am trying to write a PHP script that will process recurring payments every month, quarter, year, etc. This script will run as a nightly Cron job.
I don't want to run into a situation where somebody subscribes, say, on the 15th of a January, and then gets billed again on the 1st of February.
Is that what would happen if I checked the last payment against INTERVAL 1 MONTH? Or would it be the same as INTERVAL 30 DAY, and only process the payment again on the 15th of February, which is what I want?
Accroding to MYSQL
If you add MONTH, YEAR_MONTH, or YEAR and the resulting date has a day
that is larger than the maximum day for the new month, the day is
adjusted to the maximum days in the new month:
mysql> SELECT DATE_ADD('2009-01-30', INTERVAL 1 MONTH);
-> '2009-02-28' Date arithmetic operations require complete dates and do not work with incomplete dates such as '2006-07-00' or
badly malformed dates:
Thus if you use the 1 month built in function you do not have to worry when the last day of the month is. MYSQL does all the work for you.
I want to pick and SUM values since last wednesday until NOW() in mysql. How can I do that?
Sorry for incomplete question, by the last Wednesday I did not mean to hard-code the date, rather I want my program to run that query, so it cannot hard-code--- Needs a flexible solution. Please help...
select date_sub(now(), interval dayofweek(date_sub(now(), interval 4 day)) day);
This works on any day of the week and always returns the Wednesday which has most recently passed. On a Wednesday itself, it returns the previous Wednesday. The next day, it returns yesterday
Genesis is right (He's very right, use his suggestion), but as an intellectual exercise: This is the best pure MySQL I could think of:
SELECT * FROM TABLE
WHERE
DATE_COLUMN > DATE_SUB( NOW(), INTERVAL DAYOFWEEK(NOW()) + 3 DAY);
NOW - DAYOFWEEK => this past Saturday. Weds. is three days before that.
SELECT SUM(value) FROM table WHERE date > '2011-07-20'
You should calculate your date from your programming language (fastest solution)
I basically have a simple calendar I set up. As of now, it shows "future" events. And then the event expires that day... I would love to find a WHERE statement that I can use to have that "event" stay up for 1 day past the "post_date"
(so if I post it as Nov. 15th,) The event would show: Name of event - Nov. 15th
And It would stay active until +1 day from post_date? (Nov. 16th would be the expire date)
Here is what I have so far:
WHERE DATE(FROM_UNIXTIME(`date`)) >= DATE(NOW())
Thanks in advance...
WHERE post_date > DATE(NOW())-INTERVAL 1 DAY
and if you really want to keep your post_dates in UNIX timestamps:
WHERE FROM_UNIXTIME(post_date) > DATE(NOW())-INTERVAL 1 DAY
Change your where statement to:
WHERE DATE(FROM_UNIXTIME(`date`)) + INTERVAL 1 DAY >= CURDATE();
Also a good idea to to use real SQL dates instead of UNIX timestamps. There are functions to do calculations on them.