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
Related
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
This question already has answers here:
getting records of last 30 mins from mysql database with unix timestamp
(2 answers)
How to retrieve records for last 30 minutes in MS SQL?
(6 answers)
Closed 1 year ago.
Select CLID,
from tblname
where callStartTime < NOW() - INTERVAL 30 minute;
This is my query. I want only those clids who come within the last 30 minutes, but on running this query I am getting complete records
To make it easier to solve a problem you should break it on smaller pieces.
You could run :
mysql> select NOW() - INTERVAL 30 minute;
+----------------------------+
| NOW() - INTERVAL 30 minute |
+----------------------------+
| 2021-12-21 11:28:02 |
+----------------------------+
1 row in set (0.00 sec)
And see the date produced by the condition.
So you are asking MySQL to retrieve all the data before 2021-12-21 11:28:02 ..... where callStartTime <='2021-12-21 11:28:02'.
Change it to where callStartTime >='2021-12-21 11:28:02'.
So
Select CLID,
from tblname
where callStartTime >= NOW() - INTERVAL 30 minute;
I have a table t_windows_updates which has two columns ci_id and update_installed_on. Table will be having all the windows updates happened to all the assets in my environment.
Data will be like
ci_id| update_installed_on
1 | 1452364200000
1 | 1453055400000
2 | 1441650600000
2 | 1441650600000
2 | 1441650600000
I want to get all ci_ids for which the latest update didn't happen in the last six months.
My Query is
SELECT t.ci_id FROM `t_windows_update` t
GROUP BY t.ci_id
HAVING MAX(t.update_installed_on)<= (NOW() - INTERVAL 6 MONTH);
It is running but getting wrong results.
Your problem is the date format.
I think this is a unix format in milliseconds. So, this suggests something like:
having max(t.update_installed_on) <= UNIT_TIMESTAMP(CURDATE() - INTERVAL 6 MONTH) * 1000
I realize that the above could have overflow issues with integers, so let's go the other way:
having max(t.update_installed_on) / 1000 <= UNIT_TIMESTAMP(CURDATE() - INTERVAL 6 MONTH)
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');
So my coworker is looking at a schema which could be described as something like this:
+--------------------+-----------+
| DATETIME timestamp | INT value |
+--------------------+-----------+
Every 5 minutes a row is entered with the value for that moment.
Here's where it gets tricky. He wants to get the average of every 8 hour period within a 7 day interval.
Certainly, I can think of solutions which involve some client side code, we were wondering if it was possible to do more in SQL.
So in essence, he wants:
SELECT timestamp, value
FROM table
WHERE timestamp >= NOW() - INTERVAL 7 DAYS
AND timestamp <= NOW();
And then breaking that up into 8 hour blocks, and averaging the contents of each block. (each block should have 12 rows, and there should be 3 blocks per day).
try
SELECT avg(`value`)
FROM `table`
WHERE timestamp >= NOW() - INTERVAL 7 DAY AND timestamp <= NOW()
group by concat(date(`timestamp`), case when hour(`timestamp`) between 0 and 7 then 1
when hour(`timestamp`) between 8 and 15 then 2
else 3 end)
If you are not tied by only doing it in your request, you could try the following method of splitting the intervals before doing the request :
boundary1 = NOW()
boundary2 = NOW()
FOR i = 0 to 21 //7 days, 3 intervals of 8 hours per days
boundary1 = boundary2
boundary2 = boundary1 - seconds(8 hours)
req = "SELECT timestamp, value FROM table WHERE timestamp >= "+boundary2+" AND timestamp <= "+boundary1
ENDFOR