Récupérer tous les messages pour un ID de catégorie donné correspondant à certains paramètres

J’ai développé une requête SQL qui obtient tous les articles de mon site contenant certains mots-clés, mais j’aimerais restreindre les articles à un identifiant de catégorie spécifique. Quelqu’un sait-il comment faire, étant donné que (il semble) category_id ne fait pas partie des champs de la table « posts » ? Voici ma requête (que j’ai exécutée et qui fonctionne bien, mais elle extrait de TOUS les articles et je ne veux que les articles d’une seule catégorie) :

-- [params]
-- string_list :first_list_of_keywords
-- null string_list :second_list_of_keywords

WITH FirstFilter AS (
    SELECT *
    FROM posts
    WHERE EXISTS (
        SELECT 1
        FROM UNNEST(ARRAY[:first_list_of_keywords]) AS s(word)
        WHERE POSITION(LOWER(s.word) IN LOWER(posts.raw)) > 0
    )
), SecondFilter AS (
    SELECT *
    FROM posts
    WHERE NOT EXISTS (
        -- Assurez-vous qu'il n'y a AUCUN mot-clé manquant dans le texte de l'article
        SELECT 1
        FROM UNNEST(ARRAY[:second_list_of_keywords]) AS t(word)
        WHERE POSITION(LOWER(t.word) IN LOWER(posts.raw)) = 0
    )
)

SELECT p.*
FROM posts p
INNER JOIN FirstFilter f ON p.id = f.id
INNER JOIN SecondFilter s ON p.id = s.id

Il suffisait d’étendre la clause WHERE pour rechercher le topic_id par rapport à la table topics, qui contiendra la colonne category_id.

-- [params]
-- int :category_id
-- string_list :first_list_of_keywords
-- null string_list :second_list_of_keywords

WITH FirstFilter AS (
    SELECT *
    FROM posts
    WHERE topic_id IN (
        SELECT id FROM topics WHERE category_id = :category_id
    ) AND EXISTS (
        SELECT 1
        FROM UNNEST(ARRAY[:first_list_of_keywords]) AS s(word)
        WHERE POSITION(LOWER(s.word) IN LOWER(posts.raw)) > 0
    )
), SecondFilter AS (
    SELECT *
    FROM posts
    WHERE NOT EXISTS (
        -- Assurez-vous qu'aucun mot-clé n'est manquant dans le texte du post
        SELECT 1
        FROM UNNEST(ARRAY[:second_list_of_keywords]) AS t(word)
        WHERE POSITION(LOWER(t.word) IN LOWER(posts.raw)) = 0
    )
)

SELECT p.*
FROM posts p
INNER JOIN FirstFilter f ON p.id = f.id
INNER JOIN SecondFilter s ON p.id = s.id
4 « J'aime »

Merci, cela a fonctionné.

1 « J'aime »

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.