简体   繁体   English

Mysql Join,需要帮助格式化查询

[英]Mysql Join, need help formatting a query

I'm sorry if this is a repeat, but I can't find anything that really answers my question as it's rather specific. 很抱歉,如果这是重复的话,但由于它很具体,我找不到任何能真正回答我的问题的东西。

I am making a forum using php and mysql, and so far so good, but I want to sort the threads in each category based on the date of their most recent post. 我正在使用php和mysql创建一个论坛,到目前为止到目前为止还不错,但是我想根据其最新帖子的日期对每个类别中的线程进行排序。

My database structure is this: 我的数据库结构是这样的:

Table: threads
Fields: name, f_id, t_id

Table: posts
Fields: author, date, post, t_id, p_id

threads has the name of the thread, the f_id of the forum it is in, and it's own auto-increment thread id. 线程具有线程名称,所在论坛的f_id以及它自己的自动增量线程ID。 posts has the author, date, post, t_id of the thread it is in, and the auto-increment post id. posts具有其所在线程的作者,日期,post,t_id和自动递增的post ID。 To this end, the second post will always be a higher ID than the first post, even if a post is made in another thread in between. 为此,即使一个帖子是在其间的另一个线程中创建的,第二个帖子将始终具有比第一个帖子更高的ID。

My issue comes when I want to display all the threads in a particular forum. 当我想显示特定论坛中的所有主题时,就会出现我的问题。 I currently use this code: 我目前使用此代码:

$t_query = mysqli_query($link, "SELECT name, t_id FROM threads WHERE f_id = ".$f_id."");

echo "<table><tr><td>Name</td><td>Author</td><td>Date</td></tr>";
while($rowt = mysqli_fetch_array($t_query)) {
    $p_query = mysqli_query($link, "SELECT author, DATE_FORMAT(date, '%r on %c/%e/%Y') as date_f, p_id FROM posts WHERE t_id = ".$rowt['t_id']." LIMIT 1");
    echo "<tr><td><a href=\"../pages/post.php?t_id=",htmlentities($rowt['t_id']),"\">",htmlentities($rowt['name']),"</a></td>";
        while($rowp = mysqli_fetch_array($p_query)){
            echo "<td>",htmlentities($rowp['author']),"</td><td>",htmlentities($rowp['date_f']),"</td></tr>";
}
echo "</table>";

That works fine and all, except I can't order by date because the date is in posts and my first loop grabs from threads. 一切正常,除了我无法按日期排序,因为日期在帖子中,而且我的第一个循环是从线程抓取的。 What I need to accomplish is a join that will grab the date of the highest p_id from posts where t_id equals the t_id from threads. 我需要完成的是一个连接,它将从帖子中获取最高p_id的日期,其中t_id等于线程中的t_id。

After re-reading that it seems a bit confusing but I hope someone can help me with the join query and tweaking my php. 重新阅读后,这似乎有点令人困惑,但我希望有人可以帮助我进行连接查询和调整我的php。

Thanks, sharf. 谢谢,码头。

You just need a join with grouping and order by max: 您只需要加入一个分组并按最大顺序排序:

SELECT t.name, t.t_id FROM threads t
INNER JOIN posts p ON p.t_id = t.t_id
WHERE t.f_id = ID
GROUP BY t.name, t.t_id
ORDER BY MAX(p.date)

Assuming you have at least one post in every thread, because this query wont find empty threads - for that, you need to use an outer join. 假设每个线程中至少有一个帖子,因为此查询不会找到空线程-为此,您需要使用外部联接。

I assume you want to sort the threads listing by the date of the most recent post in it, right? 我假设您想按其中最新帖子的日期对线程列表进行排序,对吗? Join onto posts twice, once for the most recent date, and a second time to make sure no newer post exists in it. 两次加入帖子,一次是最新日期,另一次是确保没有新帖子。 For example... 例如...

SELECT t.name, t.t_id
FROM threads t
JOIN posts p1 ON (p1.t_id = t.t_id)
LEFT JOIN posts p2 ON (p2.t_id = t.t_id AND p2.p_id > p1.p_id)
WHERE p2.p_id IS NULL
ORDER BY p1.date;

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

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