简体   繁体   English

从具有唯一数据的多个表中排序MySQL SELECT数据

[英]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: 两个数据库表如下所示:

- ForumThread - -论坛线程-

  • ID [int] , shows the unique identifier for a thread entity ID [int] ,显示线程实体的唯一标识符
  • Title [string], the user-defined title of the thread 标题[string],线程的用户定义标题
  • Author [int], the ID of the user who made this thread (I know this is redundant though :P ) 作者[int],创建此线程的用户的ID(我知道这虽然是多余的:P)
  • Views [int], the amount of views on the thread 视图[int],线程上的视图数量
  • Date [int], unix epoch number, defining the time the thread was created 日期[int],Unix纪元编号,定义了线程创建的时间

- ForumPost - -论坛帖子-

  • ID [int], the unique identifier for the thread reply (NOT the same as thread ID) ID [int],线程回复的唯一标识符(与线程ID不同)
  • ThreadID [int] , the ID referring to the thread this post/reply is associated with ThreadID [int] ,此帖子/回复所关联的线程的ID
  • Poster [int], user ID of the user who made this reply 海报[int],做出此回复的用户的用户ID
  • Content [string], the content of the reply 内容[字符串],回复的内容
  • CreationDate [int] , a not-similarily-named key for when the reply was created CreationDate [int] ,创建回复时的名称不相似的密钥

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.

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