Hi,

I have 4 tables in the database. Each of these tables has a timestamp field. I want to get MIX records from these tables sorted based on timestamp.

How can I write an SQL query for this.

Right know I'm thinking of doing this using PHP sort() function. Here is how:

Code:
<?php 
	$history = array();
	
	$checkup1 = array("Checkup 1","50");
	$test1 = array("Test 1","30");
	$problem1 = array("Problem 1","70");
	$treatment1 = array("Treatment 1","77");

	$checkup2 = array("Checkup 2","45");
	$test2 = array("Test 2","55");
	$problem2 = array("Problem 2","70");
	$treatment2 = array("Treatment 2","38");

	$checkup3 = array("Checkup 3","85");
	$test3 = array("Test 3","22");
	$problem3 = array("Problem 3","70");
	$treatment3 = array("Treatment 3","90");

	$history[] = array(1394459350 - 12000, $checkup1);
	$history[] = array(1394459350 - 8000, $checkup2);
	$history[] = array(1394459350 - 4000, $checkup3);
	$history[] = array(1394459350 - 11000, $test1);
	$history[] = array(1394459350 - 7000, $test2);
	$history[] = array(1394459350 - 3000, $test3);
	$history[] = array(1394459350 - 10000, $problem1);
	$history[] = array(1394459350 - 6000, $problem2);	
	$history[] = array(1394459350 - 2000, $problem3);
	$history[] = array(1394459350 - 9000, $treatment1);
	$history[] = array(1394459350 - 5000, $treatment2);
	$history[] = array(1394459350 - 1000, $treatment3);

	
	echo "<h1>From DB</h1>";
	$table = "<table border='1' cellpadding='5'><tr><th>Date Time</th>
			<th>Activity Type</th><th>Fee</th></tr>";
	for ($i = 0; $i < sizeof($history); $i++) {
		$table .= "<tr>";
		$table .= "<td>".date("d M Y, H:i", $history[$i][0])."</td>";
		$table .= "<td>".$history[$i][1][0]."</td>";
		$table .= "<td>".$history[$i][1][1]."</td>";
		$table .= "</tr>";
	}
	$table .= "</table>";
	
	echo $table;
	
	sort($history);
	
	
	echo "<h1>Sorted</h1>";
	
	$table = "<table border='1' cellpadding='5'><tr><th>Date Time</th><th>Activity Type</th><th>Fee</th></tr>";
	for ($i = 0; $i < sizeof($history); $i++) {
		$table .= "<tr>";
		$table .= "<td>".date("d M Y, H:i", $history[$i][0])."</td>";
		$table .= "<td>".$history[$i][1][0]."</td>";
		$table .= "<td>".$history[$i][1][1]."</td>";
		$table .= "</tr>";
	}
	$table .= "</table>";
	
	echo $table;
	
?>