$insertSQL = sprintf("INSERT INTO productos_vendidos( id_producto,quantity, id_venta)
SELECT cart_items.product_id, cart_items.quantity, productos_vendidos.id_venta
FROM cart_items
LEFT JOIN productos_vendidos ON cart_items.product_id = productos_vendidos.id_producto
where productos_vendidos.id_venta =
(SELECT ventas.id
FROM ventas
LEFT JOIN productos_vendidos ON ventas.id= productos_vendidos.id_ventas ORDER BY id DESC LIMIT 1 ) ",
ventas (
`id` bigint(20) unsigned NOT NULL auto_increment,
`fecha` timestamp NOT NULL default '0000-00-00 00:00:00',
`total_t` double NOT NULL default '0',
`CEDULA` int(10) unsigned NOT NULL default '0',
`NOMBRE_CLIENTE` varchar(30) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
productos_vendidos` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`id_producto` bigint(20) unsigned NOT NULL,
`quantity` bigint(20) unsigned NOT NULL default '0',
`id_venta` bigint(20) unsigned NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `Index_2` (`id_venta`),
CONSTRAINT `FK_productos_vendidos_1` FOREIGN KEY (`id`) REFERENCES `ventas` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CONSTRAINT `FK_productos_vendidos_1` FOREIGN KEY (`id`) REFERENCES `ventas` (`id`)
CONSTRAINT `FK_productos_vendidos_1` FOREIGN KEY (`id_venta`) REFERENCES `ventas` (`id`)