Anjali Jibhakate
Anjali Jibhakate

Reputation: 75

How to select values from 2 hours ago from now

I am not having much knowledge on mysql queries.. and i'm trying to get values from database from last two hours from now ..I have searched about it and i have found some related posts too.. but unfortunately i am unable to implement the logic.. please help me solve this issue..

here is the mysql query

select * 
from `ordermaster` 
where ((
`ordermaster`.`Pick_date`
 = curdate())
 and (`ordermaster`.`Pick_time`
 <= (now() - interval 2 hour)) 
and (`ordermaster`.`Status` 
= 2))

were, Pick_Date = "2017-04-19" (today date) and Pick_Time = "10:00:00" (24 hours format)

Upvotes: 0

Views: 2550

Answers (2)

Stefano Zanini
Stefano Zanini

Reputation: 5916

You can use the between keyword

select  *
from    `ordermaster` 
where   `Pick_date` = curdate() and
        `Pick_time` between (now() - interval 2 hour) and now() and
        `Status` = 2

Edit

Based on our chat, where we found out the GoDaddy server you have your db hosted on is 12.5 hours ahead of your local time, the final query you should use is

select  *
from    `ordermaster` 
where   `Pick_date` = curdate() and
        `Pick_time` <= now() + interval 10 hour + interval 30 minute and
        `Status` = 2

Upvotes: 0

Muhammad Qasim
Muhammad Qasim

Reputation: 1712

Try the Following:

Select * From ordermaster om
Where  Concat(om.Pick_date,' ', om.Pick_time) as date Between 
       (now() - interval 2 hour) and now()
       AND Status = 2

Upvotes: 1

Related Questions