How to select top-of-the values from database with minute resolution - php

I have a mysql(i) databse that is written to every minute (usually 4 or 5 seconds after the minute).
I would like to select the values that are cleset to top-of-the-hour for the last 36 hours and I've no idea how to do it.
I've been playing with INTERVAL and DATE_ADD but have not found something that works yet. Any help would be appreciated.
Edit:
Extra info:
Table name:
temperature
Column names:
uid (AI)
time (timestamp)
probe0
probe1
probe2
probe3
probe4
Perhaps it would also be better to be
now
now -1hour
now -2hours
etc
now -36hours
FWIW, I'm currently using the following code so select ALL the data for the last 36 hours (2160 rows)
SELECT time, probe0, probe1, probe2, probe3, probe4 FROM temperature WHERE temperature.time >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE) ORDER BY temperature.time DESC

If you want to get the first record for each hour you can use MySQL's MIN() function and since it is an Aggregate Function you need to use GROUP BY to group your data by date and hour. So a sample query will look like:
SELECT
MIN(`time`) as first_for_hour, probe0, probe1, probe2, probe3, probe4
FROM `temperature`
WHERE `time` >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE)
GROUP BY DATE(`time`), HOUR(`time`);
and for reading data 36 hours back I used your WHERE clause:
WHERE `time` >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE)
I hope this will be helpful to you and here is the playground.

Related

Get Records 30 minutes before date and time

Been trying to get this to work for 2 days and this is frustrating me.
Trying to get records 30 minutes before a date/time (Format in database is datetime).
This is what I have:
select id
from tbl_events
WHERE DATE_SUB(NOW(), INTERVAL -30 MINUTE) = DATE_FORMAT(start, '%Y-%m-%d %H:%i:%s')
What the heck am I missing?
Thanks
You already use the function DATE_SUB() so within that function you can simply use INTERVAL 30 MINUTE without the minus sign.
You also don't have to format start if it is a datetime or timestamp field.
Finally you shouldn't use = because times are hardly every exactly equal.
This gives this query:
select id
from tbl_events
WHERE start < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
Probably. It's not extremely clear what you're trying to do.

PHP/mySQL select query compare time & limit query to only those within 5 minutes

I am working on a script to select milestones from the DB that are only within 5 minutes (or any other set time amount) of the query. I'm having a few problems.
The dates are stored in the database in this format:
example: 2016-12-05 21:19:00
when I run this query:
SELECT * FROM milestones WHERE status="planned" and time >= NOW()
I am able to get the results for everything in the future, but how would I go about limiting the query to say only 5 minutes for example.
You can use BETWEEN and DATE_ADD MySQL functions to achieve the desired result, e.g.:
SELECT *
FROM milestones
WHERE status="planned" and time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 MINUTE)
This would print all the entries having date between current date and next 5 minutes.
If you want to print the same for last 5 minutes, you can use the following:
SELECT *
FROM milestones
WHERE status="planned" and time BETWEEN DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND NOW()
Here's MySQL's documentation for DATE_ADD and BETWEEN.

How do I subtract time from now() in sql?

I need to post the now() time to mysql (doing this from php) minus one second (or minute). Is there a way to do this?
Thanks in advance!
select now() - interval 1 second
or use DATESUB
select DATE_SUB(now(), INTERVAL 1 second)
SQLFiddle example
If you require formatting to your date, you could use,
select TIME_FORMAT(now() - interval 1 second,'%H:%i:%s')
There are lots of websites showing this info too. Source

Query to select records from a database that were created from a certain time in the past until now

I'm relatively a newbie, would appreciate help :)
I am looking to find entry(ies) from a mysql table which were created some time between now and a certain timestamp in the past. This time in the past is stored in a variable (say $timeinthepast, a few hours ago or yesterday, whatever). And the column 'timecreated' in the table is the timestamp of the creation of entry.
Would the following work? If not, what would?
Thanks!
<?php
$query = mysql_query("SELECT * FROM table1
WHERE timecreated = DATE_ADD(NOW(), INTERVAL -$timeinthepast)");
?>
I am basing this on: Query to select records from a database that were created within the last 24 hours
As long as $timeinthepast is valid (1 MONTH, 2 HOUR, e.g.) in the sql you can try BETWEEN
$query = mysql_query("SELECT * FROM table1
WHERE timecreated BETWEEN
DATE_SUB(NOW(), INTERVAL $timeinthepast) AND NOW()");
I find that the between syntax is the most freindly to use. I have not tested this sorry :)
Select * from `table1` where `timecreated` between DATE_SUB(NOW(), INTERVAL 1 month) and NOW()
You will need to make your time in the past a whole unit of some description.

Query to select all fields inserted in the last 30 minutes actually selects every row

Basically this query should select all the fields inserted in the last 30 minutes, but it doesnt, it selects absolutely every row making my script output wrong data
SELECT count(*) FROM mytable
WHERE `time` >= DATE_SUB(UTC_TIMESTAMP, INTERVAL 30 minute)
My time field stores the time in this kind of format 2011-06-08 22:32:03
The query works, but it selects every row, not the ones inserted in the last 30 minutes.
Try
SELECT count(*) FROM mytable
WHERE `time` >= DATE_SUB(NOW(), INTERVAL 30 minute)
Add parentheses to your query - UTC_TIMESTAMP() or use NOW() - quite simpler for me.

Categories