2

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.

4 Answers4

3

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.

John Woo
  • 258,903
  • 69
  • 498
  • 492
0

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);
Devraj verma
  • 407
  • 3
  • 14
0

Use DATE_SUB and NOW() functions in query

SELECT count(*) as lasthour_count 
FROM user_otp 
WHERE datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR);
TarangP
  • 2,711
  • 5
  • 20
  • 41
phpforcoders
  • 326
  • 1
  • 9
0
datetime>=DATE_ADD(NOW(), INTERVAL -1 HOUR);
datetime>= DATE_sub(NOW(), INTERVAL 1 HOUR);

These are the two options you can use

mega6382
  • 9,211
  • 17
  • 48
  • 69
Kedar Limaye
  • 1,041
  • 8
  • 15