2011-02-11 6 views
1

Wordpress 블로그에 얼마나 많은 활동이 있었는지 체중에 가중해야합니다. 하루에 3 개의 게시물과 10 개의 댓글이 있다고 가정 해 보겠습니다. 게시물에 대해 주어지는 점수는 10이고 의견에 대해서는 단지 1 점이라고 말하면서 총 점수는 40 점입니다. 그러나 게시물 활동이 없거나 설명 활동이없는 날이있을 수 있습니다.Wordpress - 하루에 게시물 수와 의견을 가져 오기

첫 번째 아이디어는 게시물에서 댓글 테이블로의 단순한 LEFT JOIN입니다. 그러나 게시물이없는 날은 제외됩니다. 나는 MySQL의 전문가는 아니지만 연구를 해본 결과이 문제를 해결할 수있는 가장 좋은 방법은 FULL OUTER JOIN (explained by Jeff Atwood)이며, MySQL은 이것을 지원하지 않는다!

그렇다면 실제로는 a workaround이지만 실제로는 그렇지 않습니다. RIGHT OUTER JOIN이 내가 필요한 것을 반환하지 않는 것 같습니다.
여기는 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 

하지만 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 

따라서 UNION 해결 방법은 유용하지 않습니다.

내가 뭘 잘못하고 있니? 이 작업을 수행하는 더 간단한 방법이 있습니까?

감사합니다.

참고 : 게시물과 댓글을 다른 날짜에 추가해야합니다.

+0

문제를 작동하는 것 같다 생각 당신의 오른쪽 조인은 왼쪽 조인입니다 ... –

+0

그래, 그건 오타 였어. 죄송 해요. – metrobalderas

답변

0

나는 이것이 당신이 쓸 수있는 가장 좋은 쿼리가 아니라

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 
관련 문제