SQL - Calculate 30 days ago and delete - mysql

I am trying to clear a MySQL database of entries in a table that are older than 30 days.
I think I am correct by doing it like this...
DELETE from wp_rg_lead_detail WHERE date_created < '11/05/18'
But is there a way I can get the SQL statement to calculate 30 days ago automatically? That way I could set a cronjob up and have it run everyday without me having to manually enter the date.

Use curdate() to get the current date and date_add() to subtract 30 days from it.
date_add(curdate(), INTERVAL -30 DAY);

Use date_add and sysdate functions to calculate date dynamically.
Date_add(sysdate(), interval -30 days)

Related

In mySQL how would I test if NOW() is within an interval leading up into upcoming timestamp and not older than the timestamp?

For some reason, I cannot wrap my head around the syntax and logic for this problem.
My most recent iteration of the code looks like this.
SELECT DATE_ADD(start, INTERVAL 15 MINUTE) >= NOW();
While this is true for the condition if start+15 mins is greater than NOW()
it is also true for the condition if start is older than NOW(). This the part of the code I am having an issue with.
Then I know I can add some operator AND, >= or, <= with respect to an upcoming timestamp called start or NOW() and also use DATE_ADD or DATE_SUB but I can not seem to find the correct combination of the above if NOW() is within an interval leading up into upcoming timestamp.
If you want to know if we are within 15 minutes of the start time (but only if we're before the time) this will work:
NOW() BETWEEN start - INTERVAL 15 minute AND start

Recalculate date

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)

Data from current date to 30 days after current days

Why this query is not working
SELECT * FROM history WHERE DATE(date) < CURDATE() + 30
I am trying to get the data from 30 days but my query is not working.Why
What does +30 mean? Days? Years? Months? Hours? You need to use (the proper syntax) a format MySQL understands:
SELECT * FROM history WHERE DATE(date) < CURDATE() + INTERVAL 30 DAY
To get the data from today on to 30 days after current day, you've got to set an upper and an lower limit, so use:
SELECT * FROM history WHERE
date >= CURDATE()
AND
date < CURDATE() + INTERVAL 31 DAY
Please note that by not using a function on your date column you won't prohibit MySQL to use an index on this column.
The lower limit should be obvious, the upper limit means that you've got the complete day that's 30 days later than today. If you use + INTERVAL 30 DAY instead this last day is excluded from the result.
Because you're not using the right construct, try:
SELECT * FROM history WHERE DATE_ADD(date, INTERVAL 30 DAY);

date condition to retrieve data in a 24 hour time window mysql

I am using perl and DBI to query a mysql table. I need to retrieve all rows (aprox. 75,000 rows from 3 separate databases) within the past 24 hours, ideally between 12:00 am and 11:59 pm or 00:00:00 and 23:59:59.
I was using a WHERE date condition like this:
SELECT *
FROM table_name
WHERE insert_date >= DATE_SUB(NOW(), INTERVAL 1 DAY);
Then I would run my script at midnight using cron. This worked well enough, but due to a regular large volume of traffic at midnight and the size of the queries, the execution time scheduled with cron is now 3:00 am. I changed my sql to try and get the same 24 hour period from an offset like this:
SELECT *
FROM table_name
WHERE insert_date
BETWEEN DATE_SUB(DATE_SUB(NOW(), INTERVAL 3 HOUR), INTERVAL 1 DAY)
AND DATE_SUB(NOW(), INTERVAL 3 HOUR);
This seems to work fine for my purposes but I want to ask, is there is a more readable and more accurate way, using mysql, to get all rows from the past 24 hours ( between 00:00:00 and 23:59:59 time window ) once a day while running the query from an offset time? I am generally new to all of this so any critiques on my overall approach are more than welcome.
I presume insert_date is a DATETIME?
It seems pointless to go to all the trouble of building two limits and using BETWEEN. I would simply check that DATE(insert_date) is yesterday's date. So
WHERE DATE(insert_date) = CURDATE() - INTERVAL 1 DAY
BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 DAY), "%Y-%M-%d 00:00:00")
AND DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 DAY), "%Y-%M-%d 23:59:59")
You could also use Perl date formatting functions to produce the same date-time strings, and interpolate them into the query.
....
WHERE insert_date BETWEEN CURRENT_DATE - INTERVAL 1 DAY
AND
CURRENT_DATE - INTERVAL 1 SECOND;
The lower bound will be coerced to yesterday's YYYY-MM-DD 00:00:00, and this WHERE predicate will be able to make use of an index on insert_date.
Considering that DATE(NOW()) implicitly means midnight this morning, the obvious solution is to take that value and subtract a day for the start... and subtract a second for the end.
BETWEEN DATE_SUB(DATE(NOW()), INTERVAL 1 DAY)
AND DATE_SUB(DATE(NOW()), INTERVAL 1 SECOND)

MySQL "day" - how do I get an exact figure?

I am using this function to filter query results that are older than 60 days:
s.timeSubmitted >= ( CURDATE() - INTERVAL 60 DAY )
The problem is, the "60 days" part doesn't seem to be an exact figure. I want it to filter right where s.timeSubmitted is longer than 60 days, down to the exact second of s.timeSubmitted.
How do I write "60 Days" as an exact figure (down to the second)?
The problem is that CURDATE() returns a DATE type, not a DATETIME type (an instant in time). The result of subtracting an interval from a DATE is also a DATE.
Instead, try this:
s.timeSubmitted >= ( NOW() - INTERVAL 60 DAY )
This gives you what you want, because NOW() returns a DATETIME, so the result of the subtraction is also a DATETIME.
INTERVAL 60 DAY is exact - your problem is that CURDATE() isn't. It returns whole days, not the current time.
Use NOW() instead!
I usually do
now()-interval 60 day
Assuming you want the same time of day 60 days ago;
s.timeSubmitted >= ( now() - interval 60 day);
Maybe an un-necessary note in this case; 1 day ago may be 23, 24 or 25 hours ago depending on DST changes, if you want a specific number of hours as an interval, don't use a day instead of 24 hours.