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
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();
SELECT * FROM x
WHERE ts BETWEEN TIMESTAMPADD(MINUTE, -30, NOW()) AND NOW();
First of all you need to realize that timestamp() returns a unix timestamp in seconds. 3000 seconds is not 30 minutes, it should be 1800 seconds. try that
For me, what worked is following query
select * from x where (now() - ts) < 1800000
1800000 is 30 minutes, because 60000 ms is 1 minute
You'll have to use DATE_ADD() and DATE_SUB() operators for dates. Take a look at the documentation: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
SELECT * FROM (`yourdb`) WHERE `timestamp` BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();
Related
I have a column in my table that is called timestamp that is in this format: 2012-05-01 15:33:06
How do I perform a select that only pulls records within the last 15 min? I found this in the PHP manual but am not sure how to modify for 15 minutes? Can someone give me a sample?
WHERE timestamp(CURDATE(),INTERVAL 30 DAY)
Try this............
SELECT * FROM myTable
WHERE COLUMN_NAME >= NOW() - INTERVAL 15 MINUTE
try using timestampdiff
TIMESTAMPDIFF(MINUTE,`yourcolumn`,CURDATE()) = 15;
SELECT .. FROM <table_name> Where <field_name> >= (DATE_SUB(now(), INTERVAL 15 MINUTE))
My logic tells me that I should try MINUTE instead of DAY.. did you tried it?
WHERE timestamp(CURDATE(),INTERVAL 15 MINUTE)
How do I subtract 30 days from the current datetime in mysql?
SELECT * FROM table
WHERE exec_datetime BETWEEN DATEDIFF(NOW() - 30 days) AND NOW();
SELECT * FROM table
WHERE exec_datetime BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW();
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
To anyone who doesn't want to use DATE_SUB, use CURRENT_DATE:
SELECT CURRENT_DATE - INTERVAL 30 DAY
MySQL subtract days from now:
select now(), now() - interval 1 day
Prints:
2014-10-08 09:00:56 2014-10-07 09:00:56
Other Interval Temporal Expression Unit arguments:
https://dev.mysql.com/doc/refman/5.5/en/expressions.html#temporal-intervals
select now() - interval 1 microsecond
select now() - interval 1 second
select now() - interval 1 minute
select now() - interval 1 hour
select now() - interval 1 day
select now() - interval 1 week
select now() - interval 1 month
select now() - interval 1 year
Let's not use NOW() as you're losing any query caching or optimization because the query is different every time. See the list of functions you should not use in the MySQL documentation.
In the code below, let's assume this table is growing with time. New stuff is added and you want to show just the stuff in the last 30 days. This is the most common case.
Note that the date has been added as a string. It is better to add the date in this way, from your calling code, than to use the NOW() function as it kills your caching.
SELECT * FROM table WHERE exec_datetime >= DATE_SUB('2012-06-12', INTERVAL 30 DAY);
You can use BETWEEN if you really just want stuff from this very second to 30 days before this very second, but that's not a common use case in my experience, so I hope the simplified query can serve you well.
You can also use
select CURDATE()-INTERVAL 30 DAY
SELECT date_format(current_date - INTERVAL 50 DAY,'%d-%b-%Y')
You can format by using date format in SQL.
If you only need the date and not the time use:
select*from table where exec_datetime
between subdate(curdate(), 30)and curdate();
Since curdate() omits the time component, it's potentially faster than now() and more "semantically correct" in cases where you're only interested in the date.
Also, subdate()'s 2-arity overload is potentially faster than using interval.
interval is meant to be for cases when you need a non-day component.
another way
SELECT COUNT(*) FROM tbl_debug WHERE TO_DAYS(`when`) < TO_DAYS(NOW())-30 ;
This should be so easy... but is driving me mad.
UPDATE time SET time = (time - interval 130 minute) WHERE stuff=whatever;
Time is a time column only, i.e. 09:00:00.
Assuming that you would like to subtract 130 minutes from the current time, you can use addtime, like this:
UPDATE time SET time = addtime(time, '-02:10') where stuff=whatever
130 minutes is 2 hours and 10 minutes, hence the -02:10 constant.
Here is a quick demo on sqlfiddle.
Change - to , and it will work. The correct Query is:
UPDATE time SET time = (time, interval 130 minute) where stuff=whatever
If time is a datetime or a timestmap, you must use a date_sub funktion
SELECT date_sub(time, interval 130 minute) FROM ....
Otherwise you can also convert your time with UNIX_TIMESTAMP, sub it and convert with FROM_TIMESTAMP into a mysql timestamp back
There is a DATE_SUB method that works like the DATE_ADD method you are looking for.
DATE_SUB(NOW(),INTERVAL 130 MINUTE)
Check this link for more information:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-sub
I want to substract 5 seconds to a date so I have "Select Now() from Table". I obtain 2011-08-30 18:31:37.0. However, the expected output is 2011-08-30 18:31:32.0. What would be the best approach to substract seconds to a date??
SELECT NOW() - INTERVAL 5 SECOND
or
SELECT DATE_SUB(NOW(), INTERVAL 5 SECOND)
whatever you like more.
MySQL has built in date functions that are fast. Use select Date_Sub(Now(), interval 5 second);
HTH
I need to MySQL write a query where it retrieves all rows where the unix timestamp field ("added_on") is more than 6 months old.
SELECT *
FROM yourtable
WHERE added_on <= UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 6 MONTH))
SELECT * FROM mytable WHERE added_on < (NOW() - (6 * 30 * 24 * 60 * 60));
MySQL's DATE_ADD() (aka DATE_SUB) function provides your functionality :
SELECT * FROM table WHERE DATE_ADD(FROM_UNIXTIME(added_on), INTERVAL 6 MONTH) > NOW()
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
... and is quite readable :-)
select * from table
where now() - interval 6 month > from_unixtime(added_on)
SELECT * FROM foobar WHERE added_on < UNIX_TIMESTAMP() - 15778463
This isn't exactly 6 months, as its a bit different every year, but it should be close enough for every purpose (converted to seconds by Google)
You can also convert the UNIX timestamp to a "real" timestamp and use MySQL's date/time functions on it, which'll probably be more accurate and looks prettier than having an 15778463 integer hardcoded to the query (you can use INTERVAL 6 months), but it'll also be much slower than working with plain integers.