将select语句与php组合并按时间排序

时间:2016-09-02 09:18:24

标签: php

所以我有5个单独的select语句。

<?
$stmt=$db->prepare('SELECT * FROM table1 WHERE username = :username');
$stmt->bindParam(':user', $username);
$stmt->execute();
$row = $stmt->fetchAll();

$stmt=$db->prepare('SELECT * FROM table2 WHERE username = :username');
$stmt->bindParam(':user', $username);
$stmt->execute();
$row = $stmt->fetchAll();

$stmt=$db->prepare('SELECT * FROM table3 WHERE username = :username');
$stmt->bindParam(':user', $username);
$stmt->execute();
$row = $stmt->fetchAll();

$stmt=$db->prepare('SELECT * FROM table4 WHERE username = :username');
$stmt->bindParam(':user', $username);
$stmt->execute();
$row = $stmt->fetchAll();

$stmt=$db->prepare('SELECT * FROM table5 WHERE username = :username');
$stmt->bindParam(':user', $username);
$stmt->execute();
$row = $stmt->fetchAll();
?>

我需要结合所有结果,先根据时间以最新时间组织所有结果,然后根据结果来自哪个表来回显消息。

注意每个表都有一个行时间。

示例结果:

<p>This result was posted 5 minutes ago and came from table 2</p>
<p>This result was posted 10 minutes ago and came from table 5</p>
<p>This result was posted 1 day 6 hours ago and came from table 1</p>
<p>This result was posted 2 weeks ago and came from table 3</p>
<p>This result was posted 3 weeks ago and came from table 1</p>

更新

选择*是一个绝对的要求,这就是为什么使用mysql不是一个可行的解决方案。 Mysql不能执行全表连接,而是通过执行表的交叉连接来镜像实践。但是,如果你理解了交叉连接的祸患,你会理解我说这些表中的一些会为用户返回数千个结果,使得交叉连接不可行,因为交叉连接会为每个条目创建无数重复行,并且有数千个在进行交叉连接时,条目会产生数百万和数千万行。 PHP似乎是唯一的解决方案。

我需要一个解决方案,使用php结合选择并根据时间对它们进行排序而不是mysql解决方案。

2 个答案:

答案 0 :(得分:0)

您可以在每个表中定义一个新列来保存表名。然后这种查询可以解决您的问题:

  

从table1 union中选择用户名,时间戳,tableName          从table2 order by 2 desc

中选择username,timestamp,tableName

答案 1 :(得分:0)

我认为问题应该在它所属的地方解决。这显然是db问题。 但是,当您有其他问题时,您可以尝试首先从表中获取所有行到数组。应通过降序时间戳字段按顺序选择所有表。为每个表定义一个指针,显示相关表中的最新项。在你的循环中找到这些指针所指向的行中的最新记录。并在打印出信息后增加此指针。希望能帮助到你。这是一个例子:

            $stmt=$db->prepare('SELECT * FROM table1 WHERE username = :username order by timestamp desc');
            $stmt->bindParam(':username', $user);
            $stmt->execute();
            $row[0] = $stmt->fetchAll();

            $stmt=$db->prepare('SELECT * FROM table2 WHERE username = :username order by timestamp desc');
            $stmt->bindParam(':username', $user);
            $stmt->execute();
            $row[1] = $stmt->fetchAll();
            // table lengts
            $table_len[0]=sizeof($row[0]);
            $table_len[1]=sizeof($row[1]);
            //initialize every table pointers to 0 
            $table_pointer[0]=0;
            $table_pointer[1]=0;

            while ($table_pointer[0]<$table_len[0] || 
                   $table_pointer[1]<$table_len[1]){
                // find the newest time
                $time1=strtotime($row[0][$table_pointer[0]]['timestamp']);
                $time2=strtotime($row[1][$table_pointer[1]]['timestamp']);
                $tableWithNewestTime=findNewestTime($table_len,$table_pointer,$time1,$time2);

                $timeDif=time()-strtotime($row[$tableWithNewestTime][$table_pointer[$tableWithNewestTime]]['timestamp']);
                echo "This result was posted ".$timeDif." s ego and posted from table".($tableWithNewestTime+1)."<br>";
                // increase the table pointer
                $table_pointer[$tableWithNewestTime]=$table_pointer[$tableWithNewestTime]+1;
            }

            function findNewestTime(array $table_len, array $table_pointer, $time1,$time2){
                    if ($table_pointer[0]>=$table_len[0])
                        return 1;
                    if ($table_pointer[1]>=$table_len[1])
                        return 0;
                    if($time1<$time2)
                        return 1;
                    else
                        return 0;
            }