Reputation:
I Have An table As Follow
I need Data Only From Last Hour. Following query i tried using Reference.
SELECT * FROM `user_otp`
WHERE `date` = '$todate'
AND datetime > DATEADD(HOUR, -1, GETDATE())
I Dont Know This But I tried Using This Refferance
But It Not Work For Me . So Any Help Would be useful.
Upvotes: 2
Views: 5462
Reputation: 1041
datetime>=DATE_ADD(NOW(), INTERVAL -1 HOUR);
datetime>= DATE_sub(NOW(), INTERVAL 1 HOUR);
These are the two options you can use
Upvotes: 0
Reputation: 263683
DATEADD
and GETDATE()
exist in SQL Server.
In MySQL, your conditions should be:
WHERE `datetime` > DATE_ADD(NOW(), INTERVAL -1 HOUR)
While date = '$todate'
condition is redundant and should be removed.
Here's a documentation in MySQL DateTime.
Upvotes: 3
Reputation: 326
Use DATE_SUB and NOW() functions in query
SELECT count(*) as lasthour_count
FROM user_otp
WHERE datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR);
Upvotes: 0
Reputation: 406
Hi you can use DATE_SUB function for fetch last one hour data. I edited your code below -
SELECT * FROM `user_otp`
WHERE `date` = '$todate'
AND datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR);
Upvotes: 0