Ver Mensaje Individual
  #2 (permalink)  
Antiguo 06/07/2011, 07:59
leonardo_josue
Colaborador
 
Fecha de Ingreso: enero-2007
Ubicación: México
Mensajes: 2.097
Antigüedad: 17 años, 4 meses
Puntos: 447
Respuesta: Consulta a 3 tablas

Hola SubZero_mb:

Tengo algunas dudas en cuanto al problema que lamentablemente no aclaran los datos, y es con respecto a si existiera alguna noticia que se relacione con sólo alguno de los tagas (en este caso con el 23 o 51), no sé si también se tenga que presentar o solo se deben presentar aquellas noticias que se relacionen con ambos tags... para el ejemplo voy a suponer esto ultimo: creo que podría quedar más o menos así:

Código MySQL:
Ver original
  1. mysql> create table noticias (id_noticia int, titulo varchar(15),
  2.     -> texto varchar(15));
  3. Query OK, 0 rows affected (0.06 sec)
  4.  
  5. mysql> insert into noticias values (1, 'titulo 1', 'texto noticia 1'),
  6.     -> (2, 'titulo 2', 'texto noticia 2'),
  7.     -> (3, 'titulo 3', 'texto noticia 3'),
  8.     -> (4, 'titulo 4', 'texto noticia 4');
  9. Query OK, 4 rows affected (0.01 sec)
  10. Records: 4  Duplicates: 0  Warnings: 0
  11.  
  12. mysql> create table webs_relaciones (id_web int, id_x int,
  13.     -> tipo varchar(10));
  14. Query OK, 0 rows affected (0.08 sec)
  15.  
  16. mysql> insert into webs_relaciones values (1, 1, 'noticia'),
  17.     -> (1, 73, 'imagen'), (1, 3, 'noticia'), (2, 2, 'noticia'),
  18.     -> (3, 1, 'noticia'), (3, 2, 'noticia'), (3, 4, 'noticia');
  19. Query OK, 7 rows affected (0.01 sec)
  20. Records: 7  Duplicates: 0  Warnings: 0
  21.  
  22. mysql> create table tags_relaciones (id_tag int, id_x int,
  23.     -> tipo varchar(10));
  24. Query OK, 0 rows affected (0.06 sec)
  25.  
  26. mysql> insert into tags_relaciones values (23, 1, 'noticia'),
  27.     -> (30, 73, 'imagen'), (51, 1, 'noticia'), (64, 1, 'noticia'),
  28.     -> (67, 2, 'noticia'), (2, 21, 'imagen'), (98, 2, 'noticia'),
  29.     -> (45, 3, 'noticia'), (33, 3, 'noticia'), (29, 3, 'noticia'),
  30.     -> (23, 4, 'noticia'), (51, 4, 'noticia');
  31. Query OK, 12 rows affected (0.01 sec)
  32. Records: 12  Duplicates: 0  Warnings: 0
  33.  
  34. mysql> select N.* from noticias N
  35.     -> inner join webs_relaciones W
  36.     -> on W.id_x = N.id_noticia
  37.     -> inner join (
  38.     -> select id_x from tags_relaciones where id_tag in (23, 51)
  39.     -> group by id_x having count(*) = 2) T
  40.     -> on T.id_x = N.id_noticia
  41.     -> where W.id_web = 3;
  42. +------------+----------+-----------------+
  43. | id_noticia | titulo   | texto           |
  44. +------------+----------+-----------------+
  45. |          1 | titulo 1 | texto noticia 1 |
  46. |          4 | titulo 4 | texto noticia 4 |
  47. +------------+----------+-----------------+
  48. 2 rows in set (0.00 sec)

Si fuera el caso de que también quieres mostrar aquellas noticias que se relacionen sólo con uno de los tags, lo único que deberías hacer es quitar la condición HAVING del select interno (T)

Dale un vistazo y espero que te sirva...

Saludos
Leo.