Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL SELECT timestamp(now()-3000);

I'm trying to make a query which brings back results based on a timestamp, say an interval of 30 minutes.

So what I figured out is that I can

SELECT * FROM x WHERE ts BETWEEN timestamp(now()-3000) AND timestamp(now())

So this will query everything from x with timestamps in column ts within the last 30 minutes.

However, this only works after now() is past the yyyy-mm-dd HH:30:00 mark because anytime before it will result in NULL... this is rather cumbersome and I don't understand why it won't just subtract the friggin minutes from the hour!

Please help me out! I couldn't find any other method of doing a query within the last 30 minutes, that is what I'm trying to achieve.

Best regards,

John

like image 882
Theveloper Avatar asked Sep 30 '11 00:09

Theveloper


People also ask

What is current timestamp in MySQL?

MySQL CURRENT_TIMESTAMP() Function The CURRENT_TIMESTAMP() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).

How is timestamp stored in MySQL?

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME, which is stored “as is”.) By default, the current time zone for each connection is the server's time.

How do I pick a date from a timestamp?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.


2 Answers

SELECT * FROM x WHERE ts BETWEEN timestamp(DATE_SUB(NOW(), INTERVAL 30 MINUTE)) AND timestamp(NOW())
like image 172
mluebke Avatar answered Oct 29 '22 04:10

mluebke


SELECT * FROM x WHERE ts BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();
like image 29
ypercubeᵀᴹ Avatar answered Oct 29 '22 03:10

ypercubeᵀᴹ