인기 주제 배지

단순한 버전은 다음과 같은 형태가 될 수 있습니다:

SELECT 
    t.user_id,
    CURRENT_TIMESTAMP AS granted_at,
    p.id AS post_id
FROM topics t
  JOIN posts p ON p.topic_id = t.id AND p.post_number = 1
WHERE t.posts_count >= 100
  AND t.archetype = 'regular'
  AND t.deleted_at ISNULL
  AND t.user_id > 0

t.post_counts를 사용하면 현재 상태에서는 작은 액션 게시물(닫힘, 비공개 등)도 포함되므로, 얼마나 정확하기를 원하는지에 따라 달라집니다.

쿼리 내에서 ‘대상’ 게시물의 수를 세어 더 구체적으로 만들 수도 있습니다. 예를 들어 다음과 같이:

SELECT 
    t.user_id,
    CURRENT_TIMESTAMP AS granted_at,
    p.id AS post_id
FROM topics t
  JOIN posts p ON p.topic_id = t.id AND p.post_number = 1
WHERE t.id IN ( 
    SELECT 
        p.topic_id
    FROM posts p
      JOIN topics t ON t.id = p.topic_id
    WHERE t.archetype = 'regular'
      AND t.deleted_at IS NULL
      AND p.deleted_at IS NULL
      AND p.post_number <> 1
      AND p.post_type = 1
      AND p.hidden IS FALSE
     GROUP BY 1
     HAVING COUNT(*) >= 100
     )