[英]Sorting MySQL SELECT data from multiple tables with unique data
In a group project I'm doing (a forum), I'm stuck on the task of sorting forum threads by the last reply. 在我正在做的一个小组项目(一个论坛)中,我被困在最后一个答复之前对论坛主题进行排序的任务。 I know the title was somewhat confusing, but we'll try get an understanding of the scenario below.
我知道标题有些令人困惑,但是我们将尝试了解以下情况。
I have two tables, which have some foreign keys and relation keys, and I want to order the rows selected from the table containing the threads by the newest post with the associated ID for that thread. 我有两个表,其中有一些外键和关系键,并且我想按最新的帖子排序从包含线程的表中选择的行,该最新帖子具有该线程的关联ID。
The two database tables looks like this: 两个数据库表如下所示:
As you can see above, some of the table keys who should be able to relate to keys in the other table, have different names. 正如您在上面看到的,应该能够与另一个表中的键相关的某些表键具有不同的名称。 Furthermore, I need to link/group/join ForumPost.ThreadID to ForumThread.ID, but all my current efforts have only led me to getting multiple rows for each reply to a thread, meaning that a thread with eg 2 replies would show up twice after sorting the result.
此外,我需要将ForumPost.ThreadID链接/分组/加入到ForumThread.ID,但是我目前的所有工作仅使我对线程的每个回复都获得多行,这意味着具有2个回复的线程将显示两次。对结果进行排序之后。
Here is what I have tried so far: 到目前为止,这是我尝试过的:
Attempts at sorting thread by last post: 尝试按最新帖子对线程进行排序:
SELECT DISTINCT ForumPost.ThreadID,
ForumThread.*
FROM ForumThread
LEFT JOIN ForumPost
ON ForumThread.ID = ForumPost.ThreadID
GROUP BY ForumPost.ThreadID
ORDER BY ForumPost.CreationDate DESC,
ForumThread.ID DESC
LIMIT $offset, 10
SELECT ForumThread.*
FROM ForumThread
LEFT JOIN ForumPost
ON ForumThread.ID = ForumPost.ThreadID
GROUP BY ForumPost.ThreadID,
ForumThread.ID
ORDER BY ForumPost.CreationDate DESC,
ForumThread.ID DESC
LIMIT $offset, 10
SELECT DISTINCT ForumThread.*
FROM ForumThread
INNER JOIN ForumPost
ON ForumThread.ID = ForumPost.ThreadID
GROUP BY ForumThread.ID
ORDER BY ForumPost.CreationDate DESC,
ForumThread.ID DESC
LIMIT $offset, 10
Original simple query: 原始的简单查询:
SELECT *
FROM ForumThread
ORDER BY ID DESC
LIMIT $offset, 10
Would you in this case just recommend grabbing the information separately, and use a language such as PHP (obviously this is what we're using for this project) to associate the two different MySQL results and sort them then, or do you have a faster and more compact solution? 在这种情况下,您是否建议仅单独获取信息,并使用PHP之类的语言(显然这是我们在此项目中使用的语言)来关联两个不同的MySQL结果并对其进行排序,否则您会更快吗?和更紧凑的解决方案?
Note; 注意; For all those of you who recommend me to use MySQLi, I can confirm that my group have pondered whether to use MySQLi or MySQL, though our learning pensum have only covered the outdated MySQL material, making it weight out the possibility for learning MySQLi with our narrow deadline for this project.
对于所有建议我使用MySQLi的人,我可以确认我的小组已经在考虑是使用MySQLi还是MySQL,尽管我们的学习笔数仅涵盖过时的MySQL资料,这使我们可以通过我们的学习来加权学习MySQLi的可能性。该项目的截止日期很短。
This is a possibility using a correlated subquery; 这是使用相关子查询的一种可能性。 hope it helps
希望能帮助到你
SELECT
ForumThread.id, Title,
(SELECT MAX(CreationDate) FROM ForumPost
WHERE
ForumPost.ThreadID = ForumThread.id) AS lastentry
FROM ForumThread group by 1, 2 order by 3 desc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.