MYSQL Time range

Asked

Viewed 615 times

0

I need to search on a server, through a command, an interval of 2 hours of a saved data relative to the server time.

Example: An employee opens a call; this call will be saved to the bank on date X with time 12:00:00 and its status will be "OPEN". I need to pick up all the calls that haven’t been solved in a 2-hour window.

SELECT datas FROM banco WHERE DATE_SUB(CURDATE(), INTERVAL 2 HOUR) <= datas AND status='2';

The purpose of this command is to search the bank for the time of the calls and compare with the system time. If it’s been two hours, it’ll show, if not, it doesn’t show.

When I give this command, it shows me this data,

inserir a descrição da imagem aqui

The command was given at 15:03, only it showed other times, beyond 2 hours open.

1 answer

1


Your select returns this because CURDATE only returns the current date without the time. Use NOW() in the calculation as it returns current date and time:

SELECT datas FROM banco WHERE datas >= DATE_SUB(NOW(),INTERVAL 2 HOUR) AND status='2';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.