SQL Select data in the last 1 minutes - mysql

i am currently practicing my SQL skills. I wanted to get all data in the past 1 minutes.
The query is SELECT * FROM menfesses WHERE created_at >= NOW() - INTERVAL 1 MINUTE;
But somehow, it returns all data.
I have also use date_add approach and nothing works
What did i do wrong? Thanks

Probably your server time that's not what you think it is.
This work with a 5 min laps.
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE t1
(`c_date` datetime)
;
INSERT INTO t1
(`c_date`)
VALUES
(NOW() - INTERVAL 30 MINUTE),
(NOW() - INTERVAL 2 MINUTE),
(NOW() - INTERVAL 1 MINUTE)
;
Query 1:
SELECT *,NOW() FROM t1 WHERE c_date >= NOW() - INTERVAL 5 MINUTE
Results:
| c_date | NOW() |
|----------------------|----------------------|
| 2023-02-09T15:44:05Z | 2023-02-09T15:46:20Z |
| 2023-02-09T15:45:05Z | 2023-02-09T15:46:20Z |

The last five minutes must be select with a BETWEEN.
Also testing you should add
SELECT created_at, Now() FROM menfesses WHERE created_at BETWEEN NOW() - INTERVAL 5 MINUTE AND NOW()
So that have a chance to debug it correctly
Between would only give you the correct data, as your test server could have dates later then now
Edit
a fiddle demonstrates my point https://dbfiddle.uk/c5Jlko65
You maybe off on the system you have

Related

Comparing the database timestamp to the present time

I have a number of records in a MySQL database with one of the fields which tracks the time of the record creation. It is of the type 'timestamp' with the default set to CURRENT_TIMESTAMP. I am trying to access all records that are newer than 5 minutes old. I have used the following query in a PHP page:
SELECT username FROM liveusers WHERE timejoined < NOW() - INTERVAL 5 MINUTE)
For some reason, it doesn't select any records and I'm not sure how to rectify it. The PHP code is fine as it selects all records if I remove the condition. What am I doing wrong?
I suggest you to use DATE_SUB:
SELECT username FROM liveusers WHERE timejoined > DATE_SUB(NOW(),INTERVAL 5 MINUTE);
https://www.w3schools.com/sql/func_mysql_date_sub.asp
You were doing the comparison the other way arround. Newer records should have a greater date than 5 minutes ago.
First, you are comparing a timestamp against a date. You want to do first a conversion of the date, in example, using UNIX_TIMESTAMP()
5 minutes is 300 seconds. Let's imagine someone joined at 50 and NOW() is 301 (it should be "newer"). WHERE timejoined < UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE) will become -> WHERE 50 < 301 - 300 <- false
You want to write :
WHERE timejoined > UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE)
Schema (MySQL v5.7)
Query #1
SELECT UNIX_TIMESTAMP(NOW() - INTERVAL 4 MINUTE) > UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE) AS "is 4 minutes ago \"newer\" ?";
| is 4 minutes ago "newer" ? |
| -------------------------- |
| 1 |
Query #2
SELECT UNIX_TIMESTAMP(NOW() - INTERVAL 6 MINUTE) > UNIX_TIMESTAMP(NOW() - INTERVAL 5 MINUTE) AS "is 6 minutes ago \"newer\" ?";
| is 6 minutes ago "newer" ? |
| -------------------------- |
| 0 |
View on DB Fiddle

How can I set UNIX_TIMESTAMP correctly on my local?

I have a table which has a column named date_time. It is containing a unix number of the time. Something like this:
// mytable
+----+------------+
| id | date_time |
+----+------------+
| 1 | 1464499385 | -- 19 days ago
+----+------------+
-- ^ these are based on current time which is 1464566088
Also here is my query:
SELECT id,
(CASE WHEN FROM_UNIXTIME(date_time) >= CURDATE() THEN 'today'
WHEN FROM_UNIXTIME(date_time) >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN 'yesteray'
WHEN FROM_UNIXTIME(date_time) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) THEN 'in last week'
ELSE 'in last month or more'
END) as `range`
FROM mytable
WHERE 1;
The result of query above on local isn't the same as on fiddle.
on local:
As you see the result on local is yesterday and on fiddle is today. Why there is a different and how can I fix it?
Note: when I select UNIX_TIMESTAMP on local and on fiddle, there is a different.
SELECT UNIX_TIMESTAMP(); -- 1464566511 (on local)
SELECT UNIX_TIMESTAMP(); -- 1464562972 (on fiddle)
So how can I set identically?
Sql fiddle server is in a different time zone.
Take 'UTC - your timezone' and subtract that many interval hours from your timestamp
You can convert with the difference using convert_tz :
SELECT CONVERT_TZ(datetime,'+00:00','-10:00');
Convert _TZ MySql documents
You can Google time in UTC and take the difference or you can set global tinezone to your local some support on that

Mysql datetime format add 10 minutes

Hi i have table with datetime variable.
I was wondering if i can somehow change the datetime column to add 1O minutes to stored date.
Perhaps some trigger has to be involved.
Thanks for help
I like the INTERVAL expr unit notation. It feels more readable to me:
SELECT NOW(),
NOW() + INTERVAL 10 MINUTE;
+--------------------------------+-------------------------------+
| NOW() | NOW() + INTERVAL 10 MINUTE |
+--------------------------------+-------------------------------+
| August, 12 2013 14:12:56+0000 | August, 12 2013 14:22:56+0000 |
+--------------------------------+-------------------------------+
If you want to select existing rows and add 10 minutes to the result:
SELECT the_date + INTERVAL 10 MINUTE FROM tbl;
If you want to alter existing rows stored in a table, you could use:
UPDATE tbl SET the_date = the_date + INTERVAL 10 MINUTE;
If you want increase by force a value by 10 minutes while inserting, you need a trigger:
CREATE TRIGGER ins_future_date BEFORE INSERT ON tbl
FOR EACH ROW
SET NEW.the_date = NEW.the_date + INTERVAL 10 MINUTE
add 10 minute in following way
SELECT ADDTIME(now(), '1000');

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 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