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
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).
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.
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.
SELECT * FROM x WHERE ts BETWEEN timestamp(DATE_SUB(NOW(), INTERVAL 30 MINUTE)) AND timestamp(NOW())
SELECT * FROM x WHERE ts BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With