Mysql - Select rows created 1 hour ago - mysql

I try to developp a cron task (PHP-MYSQL) that will be executed every 10 minutes to send an email only on the results of my query.
On this script, i would like to select the rows created 1 hour ago. I have a datetime field which contains the creation date.
How to select rows created 1 hour ago ? (without to take care of minutes and seconds)
I tried this code below but it returns me all the rows older than 1 hour, which is not what i need
SELECT email FROM clients WHERE (date_creation < DATE_SUB(NOW(), INTERVAL 1 HOUR))

You can use this query.
SELECT email FROM clients WHERE date_creation >= DATE_SUB(NOW(), INTERVAL 1 HOUR);
it will give you all records created in hour.

Related

How to get records that are less than n minutes?

I am trying to get the records from php that every row have following table:
[created_at] => 2022-10-15 08:17:52
I want get record that are created last minute.
for that i my php file:
SELECT * FROM customer_billing_details WHERE DATE_ADD(created_at, INTERVAL 1 MINUTE) >= NOW();
but it returning the same row even after it is more than 1 minute old,
I dont able to understand what is happening.
You do not need >= NOW(). the interval will automatically handle the time interval. use something like
SELECT *
FROM `customer_billing_details`
WHERE `created_at` >= CURRENT_TIMESTAMP - INTERVAL 5 MINUTE;
Change 5 minutes to 1 or 2 according to your needs.

PHP to SQL add days to date column and post it to new date column

I to have this
Date
29/03/2018
+2
Due
31/03/2018
I am using this code , very simple and it works if i add seconds and hours, but if i go above 1 day the due column goes to 00:00:000
$sq2= "UPDATE Tickets SET Tickets.due= date(`date`)+20000;
( this currently adds 2 hours..)
how do i get past 1 day and have it work correctly?
If you want a day you can use date_add
UPDATE Tickets
SET Tickets.due= DATE(DATE_ADD(`date`, INTERVAL 1 DAY))
if you need minute hour and minute too the don't use date but
UPDATE Tickets
SET Tickets.due= DATE_ADD(`date`, INTERVAL 1 DAY)

How can i get aggregated data of every 5th minute in last 10 mins in Mysql?

I have a table which contains multiple data on same time interval for every minute. I want to aggregate data of every 5th minute in last 10 mins. I have tried all the solution provided on stack overflow but its not getting accurate data for me as none of them has tried to get data for fix interval of time.
I am using this query :
SELECT ROUND(unix_timestamp(footable.createdTime)/(60 * 5)) AS timekey, avg(mainData) as aggData
FROM footable
WHERE footable.createdTime > date_sub(now(), INTERVAL 10 MINUTE)
GROUP BY timekey
It should return max 2 records everytime but most of the time it returning 3 records.
Note:- table contains data for every minute its confirmed and this is test condition for 10 mins it could be a data of last hour. Its not a duplicate question read the description carefully.
Any help will be appreciated..!!
I figured it out myself and below is the query to solve the issue :
SET #timeStmp := UNIX_TIMESTAMP(date_sub(now(), INTERVAL 10 MINUTES));
SELECT #timeStmp := IF(((unix_timestamp(footable.createdTime) - #timeStmp) >= 295), unix_timestamp(footable.createdTime),#timeStmp) as timekey, avg(mainData) as aggData
FROM footable
WHERE footable.createdTime > date_sub(now(), INTERVAL 10 MINUTE)
GROUP BY timekey
This query will give exact minute to minute interval aggregated data on every execution. Enjoy..!!

Logic for dates in sql query for mysql

Hi i am totally confused with a date logic in my mysql query for a cron job to be run everyday at 12:00 AM
I am working on a auto listing website where the car listings are having a expiry date in mysql datetime format.
All the expired listings will be deleted from the website after 7 days from the datetime of the expiry
When the cron job will run it has do following things
Task 1 - Send an email alert to the users telling them that their listing has expired.
So I need to select all those listings which have expired since last time the cron job has been run and not include listings before that in order to send the expiry alert email only once per listing.
I tried following sql query for this task (Again confused with this as well)
SELECT car_id FROM cars WHERE expiry_date > DATE_SUB(NOW(), INTERVAL 1 DAY) AND expiry_date < NOW()
Task 2 - Will send an email alert to users telling them that listing is going to be permanently deleted after 24 hours.
So I need to select all those listings which are going to be deleted in more than 24 hours / 6 days have passed since they were expired and i need to make sure that they get minimum 24 hours time to renew them. Also i need to select / build the sql query in such a way that only those listings get selected which are going to expiry in 1 days and not other in order to avoid multiple email alerts instead of one time email alert
I tried following sql query for this task (I am totally confused with this query)
SELECT car_id FROM cars WHERE expiry_date > DATE_SUB(NOW(), INTERVAL 7 DAY) AND expiry_date < DATE_SUB(NOW(), INTERVAL 1 DAY)
Task 3 - Delete all the listings which were expired more than 7 days ago
I tried following sql query for this task
SELECT car_id FROM cars WHERE expiry_date < DATE_SUB(NOW(), INTERVAL 7 DAY)
Please help me in perfecting all the 3 queries so that the cron does it job exactly as i want. Also please let me where it has to >= (greater than or equal to) or <= (less than or equal to)
Here is the sqlfiddle table structure and couple of records (though they are not expired yet)
http://sqlfiddle.com/#!2/cfcdf
I will really appreciate the help.
Is this what you are looking for? Please try to add another column to see the differnce between expiry_date and current date time for you to get a better idea of the dates you are dealing with. Please look into some dates functions in MYSQL.
SQLFIDDLE DEMO
-- 3rd query expiry dates older than 7 days from
-- today
SELECT car_id, expiry_Date,
DATE_sub(NOW(), INTERVAL 7 DAY)
FROM cars
WHERE expiry_date <=
DATE_sub(NOW(), INTERVAL 7 DAY)
;
-- same
SELECT car_id, expiry_Date,
DATE_ADD(NOW(), INTERVAL -7 DAY)
FROM cars
WHERE expiry_date <=
DATE_ADD(NOW(), INTERVAL -7 DAY)
;
-- 2nd query going to expire in exactly 1 day
SELECT car_id, expiry_date,
Now() + interval 1 day
FROM cars
WHERE expiry_Date = Now() + interval 1 day
;
-- 1st query: expired
SELECT car_id FROM cars
WHERE expiry_date < Now()
;
-- 1st query: expired last 24 hours
SELECT car_id,DATEDIFF(expiry_date, Now())
FROM cars
WHERE expiry_Date < Now()
AND expiry_Date >= Now() - interval 1 day
;
Check out these queries
select * from cars where datediff(EXPIRY_DATE,now())=-1;
select * from cars where
datediff(DATE_ADD(EXPIRY_DATE, interval 24 hour),now())>=1 and
datediff(DATE_ADD(EXPIRY_DATE, interval 24 hour),now()) <=2;
select * from cars where datediff(expiry_date,now())<=-7;
ope they are working according to your need.
fiddle http://sqlfiddle.com/#!2/785ea/5
There is nothing significantly wrong with your queries, if you do not understand the functions that you have used then google them and read about them until you do.
There is a fundamental problem in your approach in that it relies on the cron job being run at exactly 24 hour intevals - to the milisecond - or there will be double ups and/or omissions.
You need another table to store details of when your batch program last ran; intitialise this with 1 row with a date a long time in the past so that we have a starting point.
You can get the most recent batch by SELECT MAX(date_ran) FROM BatchRecordTables. Store this in a local variable T0. Get the current time, store this in a local variable T1 (Do not use NOW() in multiple queries as they will be slightly differant times and you need them to be the same). I do not know the syntax for this is MySQL - you will have to look it up.
Your situations then become.
Send email to people whose listings have expired since that last time the cron job was run i.e. SELECT car_id FROM cars WHERE Expiry_Date BETWEEN T0 AND T1. This will only select people whoose listings have expired between this batch and the previous one.
For the second case, we need to know that these people have got the first email i.e. that their listing had expired before the last batch run so SELECT car_id FROM cars WHERE Expiry_Date BETWEEN DATE_SUB(T1, INTERVAL 6 DAY) AND T0. This will only select people whoose listings expired before the last batch (i.e. they got the exprired email) and more than 6 days ago.
Same logi applies - we want to know they got the second email. SELECT car_id FROM cars WHERE Expiry_Date BETWEEN DATE_SUB(T1, INTERVAL 7 DAY) AND DATE_SUB(T0, INTERVAL 6 DAY)
May I also suggest that you do not permenantly delete the listings but either copy them to a DeletedListings table or Flag them with a Deleted column - each has its own pros and cons. In the information age, never throw data away - you never know when it might be valuable.

How to select/count all records which happened in the last 2 hours? - MYSQL

In MYSQL i have DATE and TIME seperated. I need to count how many times one ip failed to login in the last 2 hours. If he failed too many times, then he can't login for the next 2 hours.
I just don't know which statement is the correct one (maybe none of the 2 below).
SELECT COUNT(`ip`) AS count_failed_logins FROM `failed_logins` WHERE `time`=TIME(CURTIME()+Interval 2 hour)
or
SELECT COUNT(`ip`) AS count_failed_logins FROM `failed_logins` WHERE `time`=TIMEDIFF(CURTIME()+Interval 2 hour)
You can simply do :
SELECT COUNT(`ip`) FROM `failed_logins` WHERE `time` > DATE_SUB(NOW(), INTERVAL 2 HOUR);