Jump to content

PHP-MySQl: Compare records in a table and joints matched results as a single row


avvllvva

Recommended Posts

Hi,

 

I need to compare records in a table based on the 'datetime' field, like if the difference between time is less than 15min.

 

need to combine the records(rows) as a single resultant row until the time difference between them  is less than 15min and when a record's time difference is >15min that should return as another row and so on. Please find following sample table and the required output format

 

Sample Table:

 

ID	NAME	DATETIME 
1   aaa    	2011-10-10 06:30:00
2   bbb    	2011-10-10 06:33:00
3   ccc    	2011-10-10 06:38:00
4   ddd    	2011-10-10 06:40:00
5   eee    	2011-10-10 07:10:00
6   ffff   	2011-10-10 07:14:00
7   sss    	2011-10-10 08:16:00
8   jjj   	2011-10-10 08:26:00
9   kkk   	2011-10-10 08:28:00
10  mm   	2011-10-10 09:46:00
11  ppp   	2011-10-10 09:49:00
12  qqq   	2011-10-10 09:52:00

 

 

Output Needed :

 

IDs			START DATETIME				END DATETIME			 
1,2,3,4 		2011-10-10 06:30:00			2011-10-10 06:40:00
5,6			2011-10-10 07:10:00                      2011-10-10 07:14:00
7,8,9		2011-10-10 08:16:00			2011-10-10 08:28:00
10,11,12		2011-10-10 09:46:00                      2011-10-10 09:52:00

 

You can see 1st row in the output table having IDs 1,2,3,4 coz the time difference between them is less than 15minutes (it doesn't means time difference between start and end; it is actually the  time difference between current record and previous one ; ie; ID-1 and ID-2 have difference less than 15min and ID-2 & ID-3 have difference less than 15min and ID-3 & ID-4 have difference less than 15min, so those 4 records combine together as a single row also shows their start time and end time. Then you can see diff between ID-4 and ID-5 is greater than 15 min so it should display as new row, and so on)

 

How can I acheive above Output with mysql query ??

 

Please help..

 

 

Link to comment
Share on other sites

It is possible to achieve it with MySQL if I got what you want exactly.

So what if a record is in range of 15 mins with previous one AND with the next. BUT the previous and next are not in range of each other? How do you want to have that result?

 

Edit: as mjdamato mentioned, handling the data with php would be much easier though.

Link to comment
Share on other sites

<?php
$query = mysql_query("SELECT * FROM table ORDER BY datetime ASC");
$ids = array();
$startTime = null;
while ($row = mysql_fetch_assoc($result)) {
if($startTime == null)
{
	$ids[] = $row['id'];
	$startTime = $row['datetime'];
}
else if(strtotime($row['datetime']) - strtotime($lastRow['datetime']) > 60*15)
{
	//Within 15 mins of the last row.
	$ids[] = $row['id'];
}
else
{
	//Not within 15 mins, so dump the info and start a new row.
	var_dump($ids,$startTime,$lastRow['datetime']);

	$ids = array();
	$startTime = null;
}
$lastRow = $row;
}

Something like this should work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.