user9341437
user9341437

Reputation:

Sql Query to find data only from last hour

I Have An table As Follow

enter image description here

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

Answers (4)

Kedar Limaye
Kedar Limaye

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

John Woo
John Woo

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

phpforcoders
phpforcoders

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

Devraj  verma
Devraj verma

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

Related Questions