我在为我的WordPress博客建立一个“作者”页面,列出所有当前网站贡献者以及各种其他信息,例如发布的帖子数量,最后发表的日期等寻找作者的最新职位
谷歌和Wordpress Codex指出我在MySQL中使用子查询来在一个查询中提取我需要的所有数据,并且它对于获取每个作者发布的帖子数量非常有用。
我无法工作的是找到每个作者的最新帖子的帖子ID。
说,工程没有最新的帖子目前查询:
SELECT users.ID, (SELECT count(*) FROM posts, users WHERE users.ID = posts.post_author AND posts.post_type = 'post' AND posts.post_status = 'publish') AS post_count FROM users ORDER BY post_count DESC
我试图得到最新帖子ID为每个作者( 'latest_post_ID'):
SELECT users.ID, (SELECT count(*) FROM posts, users WHERE users.ID = posts.post_author AND posts.post_type = 'post' AND posts.post_status = 'publish') AS post_count, (SELECT posts.ID FROM posts, users WHERE users.ID = posts.post_author AND posts.post_type = 'post' AND posts.post_status = 'publish' ORDER BY posts.post_date DESC LIMIT 1) AS latest_post_ID FROM users ORDER BY post_count DESC
的问题是与加子查询 - 查询将查找任何与任何作者匹配发布的帖子,而不是我想要的('users.ID = posts.post_author')。
我非常感谢它,如果有人用一些SQL-fu可以指出我滥用和/或滥用MySQL子查询的地方。
您缺少的链接正在绑定您最初选择的用户标识和子查询。 – 2009-09-01 05:52:37