Ver Mensaje Individual
  #3 (permalink)  
Antiguo 03/03/2008, 09:37
Avatar de nicolaspar
nicolaspar
 
Fecha de Ingreso: noviembre-2004
Ubicación: Villa Ballester Bs-As|Ar
Mensajes: 2.002
Antigüedad: 19 años, 5 meses
Puntos: 34
Re: Base de datos + relaciones "raras"

Gracias por responder!. Y si, hasta ahí estamos bien. Pongamos a modo de ejemplo esta db:
Código:
/*Table structure for table `tbl_bebidas` */
CREATE TABLE `tbl_bebidas` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `nombre` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `tbl_bebidas` */
insert into `tbl_bebidas` (`id`,`nombre`) values (1,'Pure'),(2,'Milanesas'),(3,'Siuza'),(4,'Asado');

/*Table structure for table `tbl_comidas` */
CREATE TABLE `tbl_comidas` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `nombre` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `tbl_comidas` */
insert into `tbl_comidas` (`id`,`nombre`) values (1,'Coca Cola'),(2,'Vino'),(3,'Agua'),(4,'Soda');

/*Table structure for table `tbl_rel_bebidas` */
CREATE TABLE `tbl_rel_bebidas` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `usuario` int(11) unsigned NOT NULL default '0',
  `bebida` int(11) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `tbl_rel_bebidas` */
insert into `tbl_rel_bebidas` (`id`,`usuario`,`bebida`) values (1,1,1),(2,1,2);

/*Table structure for table `tbl_rel_comidas` */
CREATE TABLE `tbl_rel_comidas` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `usuario` int(11) unsigned NOT NULL default '0',
  `comida` int(11) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `tbl_rel_comidas` */
insert into `tbl_rel_comidas` (`id`,`usuario`,`comida`) values (1,1,2),(2,1,3);

/*Table structure for table `tbl_usuarios` */
CREATE TABLE `tbl_usuarios` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `nombre` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `tbl_usuarios` */
insert into `tbl_usuarios` (`id`,`nombre`) values (1,'Nico'),(2,'Pepe');
Pero para levantar las comidas que tengan "relacionadas" alguna bebida a algun usuario debería hacer algo como:

Código:
select 
    b.nombre
from 
    tbl_bebidas b
where
    exists( 
        select 
            b2.bebida 
        from 
            tbl_rel_bebidas b2
        where 
            b2.bebida = b.id
            and exists(select c.usuario from tbl_rel_comidas c where c.usuario = b2.usuario )            
        )
Y esto, cuando hay mucho contenido explota!..., alguna sugenrencia al query o estructura?
__________________
Mi punto de partida es Que Bueno Lo Nuevo