doutangguan2460 2016-04-10 21:44
浏览 27

too long

I'm looking for a way to sort the results of 3 seperate queries of different tables by a timestamp field that's on each table.

$sql1 = "SELECT * FROM chores WHERE Status = 'unchecked' ORDER BY Time DESC";
$sql2 = "SELECT * FROM contacts WHERE Status = 'unchecked' ORDER BY DateTime DESC";
$sql3 = "SELECT * FROM gyms WHERE Status = 'unchecked' WHERE Endtime IS NOT
NULL ORDER BY EndTime DESC";

So say I have those 3 queries, and I want the data from the row whose timestamp field is the 3rd oldest, compared to all of the other rows in all of the other tables. Is there an easy way to do this either in a SQL Query or in PHP? I'm struggling with figuring out the logic for it.

  • 写回答

1条回答 默认 最新

  • duanjia1870 2016-04-14 21:25
    关注

    Lets assume that you have ran your queries and have three separate associative arrays containing all the rows...

    $array1, $array2, and $array3
    
    $test = array_merge($array1, $array2, $array3);
    usort($test, 'sortit');
    print_r($test);
    
    function sortit($a, $b){
        /* 
            Note, if the column name for the timestamp is not the same for each table,
            this function must know what element to compare for a proper sort.
    
            Also, this is assuming that the [timestamp] field is a linux epoch, if it is not, then
            simply do this:
    
            $a_time = strtotime($a['DateString']);
            $b_time = strtotime($b['TimeString']);
            return $a_time >= $b_time ? 1 : -1;
        */
        return $a['timestamp'] >= $b['timestamp'] ? 1 : -1;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值