How do I join to the latest record in the table?

霸气de小男生 提交于 2019-12-04 10:44:25

The problem you're hitting is the classic Ambiguous GROUP BY issue. This is particular to MySQL, because other RDBMS (and standard SQL) won't allow your query at all. Your query fails the Single-Value Rule, because you haven't listed all non-aggregated columns in the GROUP BY.

Here's a solution demonstrating my favorite way of getting greatest row per group:

SELECT f.forum_title, f.forum_id, p1.post_subject, p1.post_user, 
  p1.post_id, p1.post_date 
FROM board_forums f
LEFT JOIN board_posts p1
  ON (f.forum_id = p1.post_parentforum AND p1.post_parentpost = 0)
LEFT JOIN board_posts p2
  ON (f.forum_id = p2.post_parentforum AND p2.post_parentpost = 0 
      AND p1.post_id < p2.post_id)
WHERE p2.post_id IS NULL AND f.forum_status = 1
ORDER BY f.forum_position;

If p2.post_id IS NULL, that means no post is found in p2 which is greater than the post found in p1.

Ergo, p1 is the latest post (assuming post_id is auto-incrementing).


Re comment:

Slight problem with this. post_id with the highest ID is not necessarily the latest post.

No problem. Just use a column that is guaranteed to distinguish an earlier post from a later post. You mention post_date. In the case of ties, you'll have to break ties with another column (or columns) that will be sure to be in chronological order.

LEFT JOIN board_posts p2
  ON (f.forum_id = p2.post_parentforum AND p2.post_parentpost = 0 
    AND (p1.post_date < p2.post_date 
      OR p1.post_date = p2.post_date AND p1.post_millisecond < p2.post_millisecond))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!