Ver Mensaje Individual
  #5 (permalink)  
Antiguo 18/04/2016, 19:57
Ktulu
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: JOIN 3 tablas relacionadas, búsqueda por tags

Gracias por responder, despues de varias horas logre hacerlo en una unica consulta. Hasta ahora, todos los casos funcionaron.

Dejo el código por si a alguien mas le sirve.

Código MySQL:
Ver original
  1.     a.id,
  2.     a.titulo,
  3.     a.fecha,
  4.     a.url,
  5.     a.publicado,
  6.     autores.nombre AS autor,
  7.     secciones.descripcion AS seccion,
  8.     GROUP_CONCAT(tags.descripcion) as tags
  9. FROM   noticias a
  10.        INNER JOIN (SELECT   at.id_noticia
  11.                    FROM     tags_noticias at
  12.                             INNER JOIN noticias a
  13.                               ON a.id = at.id_noticia
  14.                             INNER JOIN tags t
  15.                               ON t.id = at.id_tag
  16.                    WHERE    t.descripcion IN ("tag1","tag2")
  17.                    GROUP BY at.id_noticia
  18.                    HAVING   Count(at.id_noticia) = 2) aa
  19.          ON a.id = aa.id_noticia
  20. JOIN tags_noticias ON tags_noticias.id_noticia = a.id
  21. JOIN tags ON tags.id = tags_noticias.id_tag
  22. JOIN autores ON autores.id = a.id_autor
  23. JOIN secciones ON secciones.id = a.id_seccion
  24. /* WHERE a.id = 58648*/
  25. /* WHERE a.id_autor = 2*/
  26. /* WHERE a.id_seccion = 22*/
  27. GROUP BY tags_noticias.id_noticia

Saludos!