简体   繁体   English

如何从2个单独的SQL表中选择最新日期?

[英]How to select the latest date from 2 separate SQL tables?

Original problem 原始问题

I have a code which with a while loop fetches a bunch of stuff, I ran against the problem that the while loop does not order correctly. 我有一个带有while循环的代码可以获取很多东西,我遇到了while循环不能正确排序的问题。

In this case it only sorts on which thread was posted latest, what I want is that it checks whether the latest thing in the database was a thread or a post. 在这种情况下,它仅排序最新发布的线程,我想要的是它检查数据库中最新的线程是发布还是发布。

$handler is my database connection variable, I use PDO. $ handler是我的数据库连接变量,我使用PDO。

My current code: 我当前的代码:

<?php
    $query = $handler->query('SELECT * FROM thread ORDER BY postdate DESC');

    while($fetch = $query->fetch(PDO::FETCH_ASSOC)){
        $rcount = $handler->query('SELECT COUNT(*) FROM threadpost WHERE t_id = ' . $fetch['t_id']);
        $queryTime = $handler->query('SELECT * FROM threadpost WHERE t_id =' . $fetch['t_id'] . ' ORDER BY postdate DESC');

        $fetchTime = $queryTime->fetch(PDO::FETCH_ASSOC);
        $rfetch = $rcount->fetch(PDO::FETCH_NUM);

        if(strtotime($fetch['postdate']) < strtotime($fetchTime['postdate'])){
            $fetch['postdate'] = $fetchTime['postdate'];
            $fetch['u_id'] = $fetchTime['u_id'];
        }
?>
<tr>
    <td><a href="<?php echo $website_url . 'thread/' . $fetch['t_id']; ?>" style="font-weight: bold;"><?php echo $fetch['title']; ?></a></td>
    <td><?php echo $rfetch[0]; ?></td>
    <td>
        <?php
            $monthNum  = substr($fetch['postdate'], 5, 2);
            $dateObj   = DateTime::createFromFormat('!m', $monthNum);
            echo $fetch['title'] . '<br />';
            echo substr($fetch['postdate'], 11, 8) . ' on ' . substr($fetch['postdate'], 8, 2) . ' ' . $dateObj->format('F') . ' ' . substr($fetch['postdate'], 0, 4) . '<br />';
            echo'by ' . $fetch['u_id'];
        ?>
    </td>
</tr>
<?php
    }
?>

I hope this makes any sense, it is hard to explain. 我希望这有意义,很难解释。

I already searched a bit and I found that I might need to use join, but I have no idea how I should do that. 我已经搜索了一下,发现可能需要使用join,但是我不知道该怎么做。


Partial solution: 部分解决方案:

SELECT th.*, (count(po.t_id) + count(th.t_id)) AS amount, max(po.postdate) AS lastdate FROM thread th INNER JOIN threadpost po ON th.t_id = po.t_id GROUP BY po.t_id ORDER BY lastdate DESC

Only now it only displays a thread if there are also posts in it. 只有现在,如果其中也有帖子,它只会显示一个线程。

You don't need to loop through and can probably do this in a single SQL statement using WHERE EXISTS like 您不需要循环,可以使用WHERE EXISTS在单个SQL语句中完成此操作,例如

SELECT * 
FROM threadpost t1 
WHERE EXISTS (SELECT 1 FROM thread WHERE t_id = t1.t_id)
ORDER BY postdate DESC;

(OR) probably using a simple join query like (OR)可能使用简单的联接查询,例如

SELECT t1.* 
FROM threadpost t1 JOIN thread t2 ON t1.t_id = t2.t_id
ORDER BY t1.postdate DESC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM