Ver Mensaje Individual
  #1 (permalink)  
Antiguo 27/12/2013, 16:35
Avatar de Aoshy
Aoshy
 
Fecha de Ingreso: noviembre-2012
Ubicación: Chiclayo
Mensajes: 11
Antigüedad: 11 años, 6 meses
Puntos: 0
Error en consulta Left Join

Buenas tardes.

Uso XAMPP 1.8.2 en Win7, agradezco de antemano por la ayuda, me encuentro con esta dificultad en mi consulta sql. tengo la relación de mis tablas de la siguiente manera.

Código:
CREATE TABLE IF NOT EXISTS `persona` (
  `id_persona` int(11) NOT NULL AUTO_INCREMENT,
  `nombre` char(100) COLLATE utf8_unicode_ci NOT NULL,
  `direccion` text COLLATE utf8_unicode_ci NOT NULL,
  `status` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id_persona`),
  KEY `AI_id_persona` (`id_persona`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Código:
CREATE TABLE IF NOT EXISTS `proveedor` (
  `id_proveedor` int(11) NOT NULL AUTO_INCREMENT,
  `id_persona` int(11) NOT NULL,
  `web` char(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `telefono` char(10) COLLATE utf8_unicode_ci DEFAULT NULL,
  `email` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id_proveedor`,`id_persona`),
  KEY `AI_id_proveedor` (`id_proveedor`),
  KEY `id_persona` (`id_persona`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Código:
CREATE TABLE IF NOT EXISTS `control_venta` (
  `id_venta` int(11) NOT NULL AUTO_INCREMENT,
  `fecha` date NOT NULL,
  `id_proveedor` int(11) NOT NULL,
  `id_persona` int(11) NOT NULL,
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id_venta`),
  KEY `AI_id_venta` (`id_venta`),
  KEY `id_proveedor` (`id_proveedor`,`id_persona`),
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Código:
CREATE TABLE IF NOT EXISTS `lineas_venta` (
  `id_linea` int(11) NOT NULL AUTO_INCREMENT,
  `id_venta` int(11) NOT NULL,
  `id_reproductor` int(11) NOT NULL,
  `id_cuy` int(11) NOT NULL,
  PRIMARY KEY (`id_linea`,`id_venta`,`id_reproductor`,`id_cuy`),
  KEY `AI_id_linea` (`id_linea`),
  KEY `id_venta` (`id_venta`),
  KEY `id_reproductor` (`id_reproductor`,`id_cuy`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Código:
CREATE TABLE IF NOT EXISTS `cuy` (
  `id_cuy` int(11) NOT NULL AUTO_INCREMENT,
  `sexo_cuy` char(1) COLLATE utf8_unicode_ci NOT NULL,
  `desc_cuy` text COLLATE utf8_unicode_ci NOT NULL,
  `prec_cuy` decimal(7,2) NOT NULL DEFAULT '0.00',
  `flag_empadre` tinyint(4) NOT NULL DEFAULT '0',
  `flag_parto` tinyint(4) NOT NULL DEFAULT '0',
  `flag_estado` tinyint(4) NOT NULL DEFAULT '0',
  `flag_condicion` tinyint(4) NOT NULL DEFAULT '0',
  `flag_destete` tinyint(4) NOT NULL DEFAULT '0',
  `flag_saca` tinyint(4) NOT NULL DEFAULT '0',
  `id_subclasificacion` int(11) NOT NULL,
  `id_jaula` int(11) NOT NULL,
  PRIMARY KEY (`id_cuy`),
  KEY `AI_id_cuy` (`id_cuy`),
  KEY `id_jaula` (`id_jaula`),
  KEY `id_subclasificacion` (`id_subclasificacion`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Código:
CREATE TABLE IF NOT EXISTS `reproductor` (
  `id_reproductor` int(11) NOT NULL AUTO_INCREMENT,
  `id_cuy` int(11) NOT NULL,
  `fecha_empadre` date NOT NULL,
  `peso_reproductor` decimal(7,2) NOT NULL,
  `num_partos` int(11) DEFAULT NULL,
  `num_crias` int(11) DEFAULT NULL,
  PRIMARY KEY (`id_reproductor`,`id_cuy`),
  KEY `AI_id_reproductor` (`id_reproductor`),
  KEY `id_cuy` (`id_cuy`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Mi consulta sql es la siguiente y deseo mostrar todas las compras de la tabla CONTROL_VENTA con los campos id_venta, fecha, nombre, telefono y SUM total

Código:
SELECT control_venta.id_venta,control_venta.fecha , persona.nombre, proveedor.telefono, SUM(cuy.prec_cuy) AS total
FROM control_venta
LEFT JOIN persona ON persona.id_persona = control_venta.id_persona
LEFT JOIN proveedor ON proveedor.id_proveedor = control_venta.id_proveedor
LEFT JOIN lineas_venta ON control_venta.id_venta = lineas_venta.id_venta
LEFT JOIN reproductor ON reproductor.id_reproductor = lineas_venta.id_reproductor
LEFT JOIN cuy ON cuy.id_cuy = lineas_venta.id_cuy
ORDER BY control_venta.id_venta
Pero al ejecutar mi consulta solo me muestra el primer registro de la tabla CONTROL_VENTA mas no los demás...sin embargo si le quito a la consulta la parte de SUM(cuy.prec_cuy) AS total me muestra todos los registros.

Por favor me podrían ayudar. Gracias