SQL - JOIN last child

时间:2014-09-30 作者:user1049961

我正在尝试进行一些自定义筛选。我需要加入最新的child post\\u作者。这似乎不太管用,它不断返回错误的孩子:

SELECT * FROM wp_posts
LEFT JOIN (
         SELECT MAX(ID) as child_ID, post_parent, post_author as child_post_author FROM wp_posts 
         WHERE post_type = \'ticket_reply\'
         GROUP BY post_parent
         ) sl ON wp_posts.ID = sl.post_parent
我错过了什么?

1 个回复
SO网友:user1049961

这就是我想到的解决方案:

SELECT child_ID, posts.post_author, wp1.post_author FROM wp_posts posts
LEFT JOIN (
         SELECT MAX(ID) as child_ID, post_parent FROM wp_posts 
         WHERE post_type = \'ticket_reply\'
         GROUP BY post_parent
         ) sl ON posts.ID = sl.post_parent
INNER JOIN wp_posts wp1 on sl.child_ID = wp1.ID
WHERE posts.ID = 1499

结束