MySQL timestamp SELECT - mysql

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)

Related

Delete records older than 24 hours or 1 day based on timestamp

I'm trying to write mysql query to delete records older than 24 hours.
The SELECT sql statement which i used is below
SELECT * FROM Request WHERE
timeStamp <= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))
Table contains lot of records older than 1 day but the result of this sql query is empty. Also it doesn't show any error message.
Timestamp field structure is
Name: timeSatamp
Type: timestamp
Default: CURRENT_TIMESTAMP
Can somebody help me to find out the mistake in this statement?
Thanks in advance!
You dont need the FROM_UNIXTIME() so this will do what you want
SELECT * FROM `ts` WHERE timeStamp <= DATE_SUB(NOW(), INTERVAL 1 DAY)
Or
SELECT * FROM `ts` WHERE timeStamp <= NOW() - INTERVAL 1 DAY
you can use DATEDIFF instead of the comparing from two date.
SELECT * FROM Request WHERE DATEDIFF(timestamp, NOW())>=1;
DATEDIFF returns a number of days between two date/timestamp.
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_datediff
Your Query should be like this:
SELECT * from Request where FROM_UNIXTIME(timeStamp) <= (NOW() - INTERVAL 1 DAY);
If the field timestamp contains 10 digits (like "1566836368") the right answer is
SELECT * from Request where FROM_UNIXTIME(timeStamp) <= (NOW() - INTERVAL 1 DAY);
like previously suggested by #akshaypjoshi
Delete 24 Hours older records using Mysql
DELETE FROM `table_name` WHERE `dateCreate` < (Now() - INTERVAL 24 HOUR);

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

Mysql Date and Time Difference

I have a table which has one field start_time with follwing records:
2011-07-26 14:30:00
2011-07-28 08:00:00
What I need to do is compare the field start_time with the current date-time and show records only if the difference between them is less than 5 minutes. It should show records of current date only
This is what I tried:
SELECT * FROM jqcalendar WHERE StartTime <= NOW() - INTERVAL 5 MINUTE
Use DATE_ADD/DATE_SUB for date-calculations: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
so StartTime <= DATE_SUB(NOW(), INTERVAL 5 MINUTE) should do the trick
use mysql function TIMEDIFF(date1, date2)
select * from jqcalendar WHERE TIMEDIFF(now(), StartDate) < 500
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff
have you tried
StartTime > NOW() - INTERVAL 5 MINUTE
? I think you just turned around your operator.
TIMESTAMPDIFF(MINUTE,start_time,now()) < 5

How to get results greater than yesterday

I have a database that stores the date for each entry with the DATETIME format. I need to only retrieve results that are greater than yesterday. I say yesterday because I need the results for the current day and forward.
Currently I have the following.
$yest = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
$yest_date = date('n-j-o', $yest);
SELECT * FROM TBL_NAME WHERE DATE_FORMAT(event_date, \"%c-%d-%Y\") > '".$yest_date."'
That does not give any results even though I know there are events. Any help would be greatly appreciated.
SELECT * FROM tbl_name WHERE event_date > DATE_SUB(NOW(), INTERVAL 1 DAY)
WHERE event_date > date(now()) - 1
Select * from tbl_name WHERE event_date > DATE_SUB(curdate(), INTERVAL 1 DAY)
This should have it start from the beginning of yesterday rather than 24hours back from the time the query is run.
If you have this run in a cron you should probably verify the timezone of the database vs the server so that you don't run it for two days back intending to run at 12:01 but actually running at 11:01 due to variance.
SELECT * FROM TBL_NAME WHERE event_date >= CURDATE()
SELECT * FROM tbl_name WHERE field_date > now()- interval 1 day;
what this one does is to select values from the first minute today until last minute today.
WHERE scheduled_start_timestamp >= date() AND scheduled_start_timestamp <= date() + 1