我有一个类似于这样的东西:
我尝试的方法是这样的:
问题在于我总是获取第一篇文章的标题,而不是最后一篇文章的标题。有没有一种方法可以获取最后插入的文章的标题?
此致敬礼
ID | post_author | post_date | ... | post_title | post_status | ... | post_type
-------------------------------------------------------------------------------
1 | 1 | 2007-08-11| ... | A | publish | ... | post
2 | 3 | 2007-08-12| ... | B | publish | ... | post
3 | 1 | 2007-08-12| ... | C | publish | ... | post
4 | 1 | 2007-08-13| ... | D | publish | ... | post
5 | 3 | 2007-08-13| ... | E | publish | ... | post
我想要做的是获取每个用户的帖子数量以及最后一篇帖子的标题和ID。根据上述数据,结果应该是:
AuthorID | TotalPosts | PostID | PostTitle
------------------------------------------
1 | 3 | 5 | E
3 | 2 | 4 | D
我尝试的方法是这样的:
我尝试过以下方法:
SELECT
p1.post_author AS Author,
count(p1.post_author) AS Posts,
p2.post_title AS Title
FROM
wp_posts AS p1
LEFT JOIN
wp_posts As p2
ON
p1.ID = p2.ID
WHERE
p1.post_type = 'post'
AND
p1.post_status = 'publish'
GROUP BY
p1.post_author
ORDER BY
Posts DESC,
p2.post_date ASC
LIMIT
2
问题在于我总是获取第一篇文章的标题,而不是最后一篇文章的标题。有没有一种方法可以获取最后插入的文章的标题?
此致敬礼
ORDER BY
。 - BulatORDER BY p2.post_date DESC
实际上在那里是不必要的。它可以被替换为AND p2.post_date = MAX(p1.post_date) ORDER BY anything
。但我没有看到任何避免使用ORDER BY
的理由。 - ypercubeᵀᴹ