SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE `cajas` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `id_establecimiento` bigint(20) unsigned NOT NULL, `id_caja_tipo` bigint(20) unsigned NOT NULL, `numero` int(11) NOT NULL, `nombre` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL, `estado` smallint(6) NOT NULL, `usuarioAlta` bigint(20) NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `cajas_id_caja_tipo_foreign` (`id_caja_tipo`), KEY `cajas_id_establecimiento_foreign` (`id_establecimiento`), ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*Data for the table `cajas` */ insert into `cajas`(`id`,`id_establecimiento`,`id_caja_tipo`,`numero`,`nombre`,`estado`,`usuarioAlta`,`created_at`,`updated_at`) values (1,1,1,1,'Caja 1',10,1,'2021-12-07 15:03:30','2021-12-07 15:03:30'), (2,1,1,2,'Caja 2',10,1,'2021-12-13 10:00:55','2021-12-13 10:00:58'), (3,2,1,1,'Caja 1 Est 2',10,1,'2021-12-13 10:01:07','2021-12-13 10:01:08'), (4,1,1,3,'Caja 3',10,1,'2021-12-13 10:01:18','2021-12-13 10:01:20'), (5,2,1,2,'Caja 2 Est 2',10,1,'2021-12-13 10:01:33','2021-12-13 10:01:35'); CREATE TABLE `cajas_eventos` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `id_caja` bigint(20) unsigned NOT NULL, `id_establecimiento` bigint(20) unsigned NOT NULL, `orden_apertura` bigint(20) unsigned NOT NULL, `fecha_apertura` datetime NOT NULL, `fecha_cierre` datetime DEFAULT NULL, `usuario_responsable` bigint(20) unsigned NOT NULL, `usuario_apertura` bigint(20) unsigned NOT NULL, `usuario_cierre` bigint(20) unsigned DEFAULT NULL, `saldo_inicial` decimal(30,2) NOT NULL, `saldo_final` decimal(30,2) DEFAULT NULL, `diferencia_apertura` decimal(30,2) DEFAULT NULL, `diferencia_cierre` decimal(30,2) DEFAULT NULL, `observaciones` text COLLATE utf8mb4_unicode_ci, `estado` smallint(6) NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `cajas_eventos_id_caja_orden_apertura_unique` (`id_caja`,`orden_apertura`), KEY `cajas_eventos_usuario_responsable_foreign` (`usuario_responsable`), KEY `cajas_eventos_usuario_apertura_foreign` (`usuario_apertura`), KEY `cajas_eventos_usuario_cierre_foreign` (`usuario_cierre`), KEY `cajas_eventos_id_establecimiento_foreign` (`id_establecimiento`), ) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; insert into `cajas_eventos`(`id`,`id_caja`,`id_establecimiento`,`orden_apertura`,`fecha_apertura`,`fecha_cierre`,`usuario_responsable`,`usuario_apertura`,`usuario_cierre`,`saldo_inicial`,`saldo_final`,`diferencia_apertura`,`diferencia_cierre`,`observaciones`,`estado`,`created_at`,`updated_at`) values (34,1,1,1,'2022-01-10 11:25:50',NULL,1,1,NULL,5000.00,21913.12,NULL,0.00,NULL,10,'2022-01-10 11:21:39','2022-01-10 11:25:50'); SELECT t3.*,t1.`saldo_final` FROM `cajas_eventos` t1 JOIN ( SELECT id_caja,MAX(`fecha_cierre`) AS MAXDATE FROM `cajas_eventos` GROUP BY `id_caja` ) t2 ON T1.id_caja = t2.id_caja JOIN `cajas` AS t3 ON t3.`estado`=10 AND t3.`id_establecimiento`=1 AND T1.id_caja = t3.id AND t1.fecha_cierre = t2.MAXDATE UNION SELECT t1.*,t2.saldo_final FROM Cajas AS t1 LEFT JOIN `cajas_eventos` AS t2 ON t1.id=t2.`id_caja` WHERE t1.`id_establecimiento` =1 AND t1.estado=10 AND t2.id IS NULL ORDER BY nombre
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear