MySQL Query to get Time Before and After specified Time - mysql

I am looking to pull data from my database for a time interval of 90 Minutes (before and after) a specified date and time. Currently I am using below query but it's returning zero records. Please help me in doing this.
SELECT *
FROM `ashwani_video_user_tbl`
WHERE `assigned_date`
BETWEEN from_unixtime( 1324363500 ) - INTERVAL 120
MINUTE AND from_unixtime( 1324363500 ) + INTERVAL 120
MINUTE
LIMIT 0 , 30
Thanks in advance

You need to use DATE_ADD and DATE_SUB
SELECT *
FROM `ashwani_video_user_tbl`
WHERE `assigned_date` BETWEEN
DATE_SUB(from_unixtime(1324363500), INTERVAL 90 MINUTE)
AND
DATE_ADD(from_unixtime(1324363500), INTERVAL 90 MINUTE)
LIMIT 0 , 30
SQLFiddle Demo

SELECT *
FROM `ashwani_video_user_tbl`
WHERE `assigned_date`
BETWEEN 1324363500 - 60*90 AND 1324363500 + 60*90
LIMIT 0 , 30
90 - is number of minutes, you can change this value.

Related

sql query to fetch the records of next 30 days

Here is my problem, I want to fetch next 30 days records from the table. I have a field in my table. For ex: In my table I have resource_date, In this column I have many records from 2013-02-05 to 2015-10-10. Say, If I logged into the website today(Today's Date is- 16/01/2015, It should fetch record for next 15 days and so on). How to do this? Thanks in advance
One way to do it
SELECT *
FROM table1
WHERE resource_date >= CURDATE() + INTERVAL 1 DAY -- skip today
AND resource_date < CURDATE() + INTERVAL 17 DAY -- 15 days starting tomorrow
Here is a SQLFiddle demo
In MySQL, you can use the NOW() function to get the current DATETIME, and the INTERVAL keyword to get intervals of time.
So, to get the records where resource_date is within the next 30 days, you would use:
SELECT *
FROM `my_table_name`
WHERE `resource_date` >= NOW()
AND `resource_date` < NOW() + INTERVAL 1 MONTH
;
In practice, you should rarely use SELECT *, and you should consider adding a LIMIT to this query to prevent your application from returning a result set that is "too large".
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
...
WHERE
'resource_date'> NOW() AND
'resource_date'< DATE_ADD(NOW(), INTERVAL 31 DAY);
Careful I think now() does minutes and hours so you miss a portion of a day.
WHERE resource_date >= CURDATE() AND resource_date <= DATE_ADD(CURDATE(), interval 15 DAY)

How to subtract 30 days from the current datetime in mysql?

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 ;

MySQL select a date between two dates using the time now

I am trying to select a row from a table assuming that the kick off time is within an hour's range of the current time.
The table is just an id and a datetime field.
SELECT * FROM kick_offs WHERE NOW() BETWEEN (DATE_SUB(`time`, INTERVAL 30 MINUTES)) AND (DATE_ADD(`time`, INTERVAL 30 MINUTES))
SELECT * FROM kick_offs WHERE `time` BETWEEN (DATE_SUB(NOW(), INTERVAL 30 MINUTES)) AND (DATE_ADD(NOW(), INTERVAL 30 MINUTES))
These two queries both fail. I'm not really sure why. The server is running MySQL 5.0. What am I doing wrong?
Not sure where you got your SQL from, but this should do it I think.
SELECT * FROM kick_offs WHERE `time` < DATE_ADD(NOW(), INTERVAL 1 HOUR) AND `time`>= NOW()
See:
Display rows from MySQL where a datetime is within the next hour

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
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();

Check if timestamp is after 12 hours

I'm looking to display listings from my database where the row creation (which is a timestamp) is after 12 hours from "NOW".
I've found a lot of documentation on within a timeframe, e.g:
WHERE `date` <= DATE_SUB(now(), INTERVAL 12 HOUR)
But I can't figure out how to SELECT when a row is after 12 hours. (Keeping everything that was created within 12 hours not selected)
Any help would be really appreciated,
Thanks!
UPDATED
If you need only records that created more than 12 hours ago
select * from table where creation <= NOW() - INTERVAL 12 HOUR
If you need only records that created during last 12 hours
select * from table where creation >= NOW() - INTERVAL 12 HOUR
select * from table where creation > unix_timestamp() + 43200
Try to use:
WHERE date BETWEEN DATE_SUB(now(), INTERVAL 12 HOUR) AND now();