[英]Wordpress - Get number of posts AND comments by day
I need to weight how much activity has been in a Wordpress blog. 我需要权衡Wordpress博客中有多少活动。 Say, some day there are 3 posts and 10 comments, the points awarded for a post is 10 and just 1 for a comment, then said day had 40 points in total. 假设某天有3条帖子和10条评论,某条帖子的得分为10分,而评论的得分为1分,则表示一天总共获得40分。 However, there might be some days with no post activity or with no comment activity. 但是,可能有几天没有发布活动或评论活动。
My first idea was a simple LEFT JOIN
from the posts to the comments table. 我的第一个想法是从帖子到评论表的简单LEFT JOIN
。 However, this will exclude days without posts. 但是,这将排除没有帖子的日子。 I'm no MySQL guru, but I've been researching and it seems that the best way to solve this is a with FULL OUTER JOIN
( explained by Jeff Atwood ), but MySQL doesn't suppor this! 我不是MySQL专家,但我一直在研究,看来解决此问题的最佳方法是使用FULL OUTER JOIN
( 由Jeff Atwood解释 ),但是MySQL并不支持此功能!
Then, there actually is a workaround , but it's not working for me. 然后,实际上有一个解决方法 ,但是它对我不起作用。 It seems that the RIGHT OUTER JOIN
is not returning what I need. 似乎RIGHT OUTER JOIN
没有返回我需要的东西。
Here's the LEFT
one, it works pretty good. 这是LEFT
一个,效果很好。
SELECT
DISTINCT DATE(post_date) AS day,
COUNT(ID) AS post_total,
COUNT(comment_ID) as comment_total,
(COUNT(ID)*10 + COUNT(comment_ID)*1) AS total
FROM wp_posts
LEFT OUTER JOIN wp_comments ON
DATE(post_date) = DATE(comment_date)
GROUP BY day ORDER BY total DESC
But something's wrong with the RIGHT
one. 但是,什么是错的与RIGHT
之一。
SELECT
DISTINCT DATE(post_date) AS day,
COUNT(ID) AS post_total,
COUNT(comment_ID) as comment_total,
(COUNT(ID)*10 + COUNT(comment_ID)*1) AS total
FROM wp_posts
RIGHT OUTER JOIN wp_comments ON
DATE(post_date) = DATE(comment_date)
GROUP BY day ORDER BY total DESC
Hence, the UNION
workaround is useless. 因此, UNION
解决方法是没有用的。
What am I doing wrong? 我究竟做错了什么? Is there a simpler way to do this? 有没有更简单的方法可以做到这一点?
Thanks. 谢谢。
Note: You'll have to add some posts and comments in different dates. 注意:您必须在不同的日期添加一些帖子和评论。
I think this isn't the best query you can write but seems to work 我认为这不是您可以编写的最佳查询,但似乎可以
CREATE VIEW commentsCount (date, counter) AS
SELECT
DISTINCT DATE(comment_date) AS date,
IFNULL(COUNT(comment_ID),0) AS total
FROM wp_comments
GROUP BY date ORDER BY total DESC
CREATE VIEW postsCount (date, counter) AS
SELECT
DISTINCT DATE(post_date) AS date,
IFNULL(COUNT(ID),0) AS total
FROM wp_posts
GROUP BY date ORDER BY total DESC
SELECT
postsCount.date,
IFNULL(postsCount.counter,0),
IFNULL(commentsCount.counter,0),
(IFNULL(postsCount.counter,0)*10 + IFNULL(commentsCount.counter, 0))
FROM commentsCount RIGHT JOIN postsCount
ON DATE(postsCount.date) = DATE(commentsCount.date)
GROUP BY postsCount.date
union
SELECT
commentsCount.date,
IFNULL(postsCount.counter,0),
IFNULL(commentsCount.counter,0),
(IFNULL(postsCount.counter,0)*10 + IFNULL(commentsCount.counter, 0))
FROM commentsCount LEFT JOIN postsCount
ON DATE(postsCount.date) = DATE(commentsCount.date)
GROUP BY commentsCount.date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.