Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » Mysql »

[SOLUCIONADO] Error en consulta Left Join

Estas en el tema de Error en consulta Left Join en el foro de Mysql en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 27/12/2013, 16:35
Avatar de Aoshy  
Fecha de Ingreso: noviembre-2012
Ubicación: Chiclayo
Mensajes: 11
Antigüedad: 11 años, 5 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
  #2 (permalink)  
Antiguo 29/12/2013, 06:52
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Error en consulta Left Join

Antes del ORDER BY pon

GROUP BY control_venta.id_venta,control_venta.fecha , persona.nombre, proveedor.telefono
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.
  #3 (permalink)  
Antiguo 25/01/2014, 08:45
Avatar de Aoshy  
Fecha de Ingreso: noviembre-2012
Ubicación: Chiclayo
Mensajes: 11
Antigüedad: 11 años, 5 meses
Puntos: 0
Respuesta: Error en consulta Left Join

Buenas tardes

Gracias quimfv por responder a mi consulta, te comento que solucione mi dificultad hace un tiempo ya de como realizar mi consulta, para esto tube que cambiar la estructura de mi tabla o mejor dicho cambiar mi MER.... y cambiar mi consulta para que me muestre las compras y el total correspondiente ya que era lo que deseaba.

Entonces mi consulta seria esta

Código SQL:
Ver original
  1. SELECT control_venta.id_venta, control_venta.fecha, control_venta.observacion, persona.nombre, proveedor.telefono,
  2. (SELECT SUM(lineas_venta.precio) FROM lineas_venta WHERE control_venta.id_venta = lineas_venta.id_venta) AS total
  3. FROM control_venta
  4. LEFT JOIN persona ON (control_venta.id_persona = persona.id_persona)
  5. LEFT JOIN proveedor ON (control_venta.id_proveedor = proveedor.id_proveedor)
  6. WHERE control_venta.STATUS = 0
  7. ORDER BY control_venta.id_venta

Para esto modifique mi tabla lineas_venta colocanto el precio aqui y no en la tabla cuy

Código SQL:
Ver original
  1. CREATE TABLE IF NOT EXISTS `lineas_venta` (
  2.   `id_linea` INT(11) NOT NULL AUTO_INCREMENT,
  3.   `precio` DECIMAL(7,2) NOT NULL,
  4.   `id_venta` INT(11) NOT NULL,
  5.   `id_reproductor` INT(11) NOT NULL,
  6.   `id_cuy` INT(11) NOT NULL,
  7.   PRIMARY KEY (`id_linea`),
  8.   KEY `id_venta` (`id_venta`),
  9.   KEY `id_reproductor` (`id_reproductor`,`id_cuy`)
  10. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1

Código SQL:
Ver original
  1. CREATE TABLE IF NOT EXISTS `cuy` (
  2.   `id_cuy` INT(11) NOT NULL AUTO_INCREMENT,
  3.   `sexo_cuy` CHAR(1) COLLATE utf8_unicode_ci NOT NULL,
  4.   `desc_cuy` CHAR(250) COLLATE utf8_unicode_ci NOT NULL,
  5.   `flag_empadre` INT(11) NOT NULL DEFAULT '0',
  6.   `flag_parto` INT(11) NOT NULL DEFAULT '0',
  7.   `flag_estado` INT(11) NOT NULL DEFAULT '0',
  8.   `flag_condicion` INT(11) NOT NULL DEFAULT '0',
  9.   `flag_destete` INT(11) NOT NULL DEFAULT '0',
  10.   `flag_saca` INT(11) NOT NULL DEFAULT '0',
  11.   `id_jaula` INT(11) NOT NULL,
  12.   `id_subclasificacion` INT(11) NOT NULL,
  13.   PRIMARY KEY (`id_cuy`),
  14.   KEY `AI_id_cuy` (`id_cuy`),
  15.   KEY `id_jaula` (`id_jaula`),
  16.   KEY `id_subclasificacion` (`id_subclasificacion`)
  17. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1

Bueno gracias por la ayuda

Etiquetas: campo, join, left, null, registro, select, sql, tabla
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:21.