다음은이 작업을 수행하는 기본 방법입니다. 이 예에서 각 테이블은 'id'와 'timestamp'필드를 가지며 timestamp 필드로 결과를 정렬하려고합니다.
<?
function bubblesort(array $items) {
// Sorts the items in a two-dimensional array based on a
// timestamp in the first field of the arrays passed.
do{
$flag = false;
for ($i = 0; $i < (count($items) - 1); $i++) {
if(strtotime($items[$i][0]) > strtotime($items[$i + 1][0])) {
$swap = $items[$i];
$items[$i] = $items[$i + 1];
$items[$i + 1] = $swap;
$flag = true;
}
}
}
while($flag);
return $items;
}
$results = array();
// Get results for the first table
$result = mysql_query("SELECT `timestamp`, `id` FROM `table1`;");
if (!$result)
die(mysql_error());
while ($row = mysql_fetch_array($result))
$results[] = array($row[0], $row[1]);
// Get results for the second table
$result = mysql_query("SELECT `timestamp`, `id` FROM `table2`;");
if (!$result)
die(mysql_error());
while ($row = mysql_fetch_array($result))
$results[] = array($row[0], $row[1]);
// Get results for the third table
$result = mysql_query("SELECT `timestamp`, `id` FROM `table3`;");
if (!$result)
die(mysql_error());
while ($row = mysql_fetch_array($result))
$results[] = array($row[0], $row[1]);
// Sort the joint results
$results = bubblesort($results);
?>
물론 각 쿼리는 개별 테이블에 맞게 조정되어야합니다. 버블 정렬 알고리즘을 사용했는데, 예상되는 결과의 양에 따라 다른 정렬 알고리즘이 더 좋을 수도 있습니다.
세 개의 SELECT 쿼리를 하나의 UNION (the documentation 참조)을 사용하여 병합하는 것이 더 좋을 수 있지만 필요한 쿼리를 알려주는 테이블의 구조에 대해 충분히 알지 못합니다.
예, 내 알고리즘과 동일하게 페이지를 분할하지 않은 것으로 설명합니다. 내 임무는 좀 더 복잡하고 해결할 다른 접근법을 찾고 있습니다. –
아, 죄송합니다. 질문에 대해 오해했습니다. 다시 읽은 후에는 지금하는 것보다 더 나은 방법을 생각할 수 없습니다. – EdoDodo