Diego
Diego

Reputation: 105

PHP selecting Mysql every x minutes

I have a table generated of sensor readings every +- 20 seconds. I want to display this data in a chart but since I don't need precision, how can I reduce my row selections by say, every 5 minutes? My table look like this, note that it's +-20s and time column doesn't finish with round time say.. 23:46:00...

T1      T2      Height       Time
-------|-------|-------|----------------
23.25  |24.50  | 3.71  | 2017-02-14 23:46:15 
23.25  |24.50  | 3.70  | 2017-02-14 23:46:35 
23.50  |23.50  | 3.71  | 2017-02-14 23:46:55 
23.75  |22.50  | 3.69  | 2017-02-14 23:47:15  
23.25  |24.50  | 3.70  | 2017-02-14 23:47:36 
23.50  |23.50  | 3.71  | 2017-02-14 23:47:56 
23.75  |22.50  | 3.69  | 2017-02-14 23:48:15

I suppose I can select 1 row every 15 rows on table (20s x 5 mins) for a 5 minute interval.. but how do I do this (I'm fresh on php and MySQL) any help will be appreciated.

my desired results are literally 1 row and jump 14 rows.

23.25 |24.50 | 3.71 | 2017-02-14 23:46:15  
23.25 |24.50 | 3.71 | 2017-02-14 23:51:15  
23.25 |24.50 | 3.71 | 2017-02-14 23:56:15  
etc... 

Upvotes: 0

Views: 103

Answers (1)

mfka
mfka

Reputation: 108

Maybe it helps!

$conn = mysql_connect('localhost', $user_name, $user_pass, $db_name);

$num_rows = mysql_num_rows(mysql_query($conn, 'Select * FROM table_name'));

$offset = 0;
$result = array();
while ($offset < $num_rows) {
    $result[] = mysql_query($conn, 'SELECT time FROM table_name LIMIT 1 OFFSET ' . $offset);
    $offset += 15;
}

Upvotes: 1

Related Questions