Pesquisar / Assistir por Tags popula apenas Tópicos com a tag, não respostas

Você pode referenciar uma tag em uma postagem, mas apenas um tópico pode ser realmente marcado como tal.

No entanto, se você tiver o plugin Data Explorer, poderá usar uma consulta como esta para listar aquelas em que a tag foi vinculada em uma postagem:

-- [params]
-- string :hashtag
-- date :start_date
-- date :end_date 

WITH target_posts AS (
  SELECT 
    p.id AS post_id,
    t.category_id,
    p.created_at
  FROM posts p
  JOIN topics t ON p.topic_id = t.id
  JOIN users u ON u.id = p.user_id
  WHERE t.deleted_at IS NULL
    AND t.archetype = 'regular'
    AND p.deleted_at IS NULL
    AND p.post_type = 1
    AND p.created_at::date BETWEEN :start_date AND :end_date
    AND p.post_number <> 1
)

SELECT
  tp.category_id,
  tp.post_id,
  tp.created_at::date
FROM target_posts tp
LEFT JOIN post_search_data psd ON psd.post_id = tp.post_id
WHERE psd.search_data @@ TO_TSQUERY(:hashtag)
ORDER BY tp.created_at DESC

4 curtidas