2010-01-14 6 views
2

Je travaille sur un système de recherche/tag. Ma requête originale que j'ai écrite était pour quand je stockais «titre», «description» et une colonne de «balises» séparée par virgule dans mon article/table vidéo. J'ai depuis réalisé l'avantage de normaliser mes tags. J'ai maintenant trois tables pour traiter ...mysql - retourne les résultats groupés dans une colonne

tbl_Articles

  • article_id
  • titre
  • Description
  • contenu

tbl_tag_index

  • tag_id (id primaire de substitution)
  • de tag_type (est égal à 1 pour tbl_Articles, 2 pour tbl_videos)
  • tag_word_id (voir tableau ci-dessous)
  • tag_target_id (article_id/video_id - dépend de tag_type)

tbl_tag_word

  • tag_word_id
  • tag_word (enfin la balise réelle)

Cette requête renvoie les balises ... seul problème est-il les renvoie des lignes comme différentes. Je suppose que je devrais les résultats être regroupés sur la même ligne pour que ma requête de recherche peut fonctionner

SELECT * 
    FROM `tbl_articles` A 
    JOIN `tag_index` I ON A.article_id = I.tag_target_id 
    JOIN tag_word W ON I.tag_word_id = W.tag_word_id 
WHERE I.tag_type_id = 1 

Voici ma vieille requête de recherche

SELECT *, 
(
(CASE WHEN `description` LIKE '%hotel%' THEN 1 ELSE 0 END) + 
(CASE WHEN `description` LIKE '%london%' THEN 1 ELSE 0 END) + 
(CASE WHEN `description` LIKE '%lazy%' THEN 1 ELSE 0 END) + 
(CASE WHEN `description` LIKE '%dog%' THEN 1 ELSE 0 END) + 

(CASE WHEN `title` LIKE '%hotel%' THEN 1 ELSE 0 END) + 
(CASE WHEN `title` LIKE '%london%' THEN 1 ELSE 0 END) + 
(CASE WHEN `title` LIKE '%lazy%' THEN 1 ELSE 0 END) + 
(CASE WHEN `title` LIKE '%dog%' THEN 1 ELSE 0 END) + 

(CASE WHEN `tags` LIKE '%hotel%' THEN 1 ELSE 0 END) + 
(CASE WHEN `tags` LIKE '%london%' THEN 1 ELSE 0 END) + 
(CASE WHEN `tags` LIKE '%lazy%' THEN 1 ELSE 0 END) + 
(CASE WHEN `tags` LIKE '%dog%' THEN 1 ELSE 0 END) 

) AS relevance 
FROM `tbl_Articles` 
WHERE `description` LIKE '%hotel%' 
    OR `description` LIKE '%london%' 
    OR `description` LIKE '%lazy%' 
    OR `description` LIKE '%dog%' 
    OR `title` LIKE '%hotel%' 
    OR `title` LIKE '%london%' 
    OR `title` LIKE '%lazy%' 
    OR `title` LIKE '%dog%' 
    OR `tags` LIKE '%hotel%' 
    OR `tags` LIKE '%london%' 
    OR `tags` LIKE '%lazy%' 
    OR `tags` LIKE '%dog%' 
ORDER BY relevance DESC 
LIMIT 0 , 10; 
+0

Mise à jour basé sur un commentaire sur le calcul de la pertinence de l'étiquette. –

Répondre

3

Utilisation:

SELECT A.*, 
     GROUP_CONCAT(DISTINCT w.tag_word ORDER BY w.tag_word ASC SEPARATOR ',') AS tags, 
     COUNT(DISTINCT w.tag_word) + 
     (CASE WHEN `description` LIKE '%hotel%' THEN 1 ELSE 0 END) + 
     (CASE WHEN `description` LIKE '%london%' THEN 1 ELSE 0 END) + 
     (CASE WHEN `description` LIKE '%lazy%' THEN 1 ELSE 0 END) + 
     (CASE WHEN `description` LIKE '%dog%' THEN 1 ELSE 0 END) + 

     (CASE WHEN `title` LIKE '%hotel%' THEN 1 ELSE 0 END) + 
     (CASE WHEN `title` LIKE '%london%' THEN 1 ELSE 0 END) + 
     (CASE WHEN `title` LIKE '%lazy%' THEN 1 ELSE 0 END) + 
     (CASE WHEN `title` LIKE '%dog%' THEN 1 ELSE 0 END) AS relevance 
    FROM tbl_articles A 
    JOIN tag_index I ON A.article_id = I.tag_target_id 
    JOIN tag_word W ON I.tag_word_id = W.tag_word_id 
WHERE I.tag_type_id = 1 
    AND w.tag_word IN ('hotel', 'london', 'lazy', 'dog') 
GROUP BY a.article_id, a.title, a.description, a.content 
ORDER BY relevance DESC 
+0

wow, c'est fantastique! Comment pourrais-je intégrer le titre et la description? – Mark

+0

désolé, peut-être que je n'étais pas clair dans mon message, je souhaite toujours rechercher la même chose, seulement au lieu de les étiquettes étant une colonne, ils sont dans une table séparée. Je vous remercie. – Mark

+0

Je l'ai eu de travail ... Merci. – Mark

Questions connexes