Created
August 14, 2012 19:18
-
-
Save netojoaobatista/3351926 to your computer and use it in GitHub Desktop.
Número de comentários em posts, agrupado pela categoria. E o número de comentário no período anterior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET @startDate=DATE("2012-07-01"); | |
SET @endDate:=DATE("2012-07-31"); | |
SET @diff:=@diff:=DATEDIFF(@startDate, @endDate); | |
SET @previousStartDate:=ADDDATE(@startDate, @diff); | |
SET @previousEndDate:=ADDDATE(@endDate, @diff); | |
SELECT | |
@startDate AS `currentStartDate`, | |
@endDate AS `currentEndDate`, | |
@previousStartDate AS `previousStartDate`, | |
@previousEndDate `previousEndDate`, | |
COUNT(*) AS `currentTotal`, | |
(SELECT | |
COUNT(*) | |
FROM `wordpress`.`wp_comments` AS `c2` | |
LEFT JOIN `wordpress`.`wp_posts` AS `p2` ON(`p2`.`ID`=`c2`.`comment_post_ID`) | |
LEFT JOIN `wordpress`.`wp_term_relationships` AS `r2` ON(`r2`.`object_id`=`p2`.`ID`) | |
LEFT JOIN `wordpress`.`wp_term_taxonomy` AS `x2` ON(`x2`.`term_taxonomy_id`=`r2`.`term_taxonomy_id`) | |
LEFT JOIN `wordpress`.`wp_terms` AS `t2` ON(`t2`.`term_id`=`x2`.`term_id`) | |
WHERE | |
`t2`.`term_id`=`t`.`term_id` AND | |
`c2`.`comment_date` BETWEEN | |
@previousStartDate AND | |
@previousEndDate | |
GROUP BY `t2`.`term_id`) AS `previousTotal`, | |
`t`.`name` | |
FROM `wordpress`.`wp_comments` AS `c` | |
LEFT JOIN `wordpress`.`wp_posts` AS `p` ON(`p`.`ID`=`c`.`comment_post_ID`) | |
LEFT JOIN `wordpress`.`wp_term_relationships` AS `r` ON(`r`.`object_id`=`p`.`ID`) | |
LEFT JOIN `wordpress`.`wp_term_taxonomy` AS `x` ON(`x`.`term_taxonomy_id`=`r`.`term_taxonomy_id`) | |
LEFT JOIN `wordpress`.`wp_terms` AS `t` ON(`t`.`term_id`=`x`.`term_id`) | |
WHERE | |
`c`.`comment_date` BETWEEN @startDate AND @endDate AND | |
`x`.`taxonomy`="category" | |
GROUP BY `t`.`term_id` | |
ORDER BY `currentTotal` DESC, `previousTotal` DESC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. :-D