I am trying to create this query:
select bonos.codBono, users.nombre, bonos.tipo, bonos.minutos, bonos.precio,
contratan.tiempoRestanteBono, contratan.bonoSolicitado as bonoSolicitado,
contratan.tiempoSolicitado
from contratan
INNER join users on contratan.usuario = users.id
INNER join bonos on contratan.bono = bonos.codBono
where activo = 0
and (SELECT tipo from bonos, contratan where contratan.bonoSolicitado = bonos.codBono )
And I need it to show the type of bonus requested. In contratan I save the requested bonus code and I am trying to get it to show me the type of that bonus... but the code always appears. This is to update a table, so that when the user changes the voucher, the type is changed.
The output result should be something like this:
codBono nombre tipo minutos precio tiempoRestanteBono bonoSolicitado tiempoSolicitado
6 David Serrano Alonso 10Min 10 9.00 1(aquí es donde debe aparecer el tipo) 30
It's that later, I make a dynamic table with vueJS and I can't access the bonusRequested property and for the name of the new bonus that the client requests to appear, I need that query or something similar. I don't know if I'm explaining myself correctly. I attach the structure of my DB
users table:
CREATE TABLE `users` (
`id` bigint(20) UNSIGNED NOT NULL,
`nif` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`nombre` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email_verified_at` timestamp NULL DEFAULT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`direccion` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`perfil` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'usuario',
`telefono` int(11) DEFAULT NULL,
`remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Volcado de datos para la tabla `users`
--
INSERT INTO `users` (`id`, `nif`, `nombre`, `email`, `email_verified_at`, `password`, `direccion`, `perfil`, `telefono`, `remember_token`, `created_at`, `updated_at`) VALUES
(1, '45921676z', 'David Serrano Alonso', '[email protected]', NULL, '$2y$10$nZcz5O3MaE/F/ML0QSC7M.1/fe.6QYdd11Faa1Wnsh.E4TiZR3xcW', NULL, 'usuario', 652138927, NULL, '2020-11-20 09:19:46', '2020-11-20 09:19:46'),
(2, '45921676z', 'Admin', '[email protected]', '2020-11-20 10:29:35', '$2y$10$wxOoOy1hlc8JYnozGKy.ReNwT3Q24akiltSyfrGYJ39wjud72lH7a', NULL, 'tecnico', NULL, NULL, NULL, NULL);
--
Bonus History Table:
CREATE TABLE `historialbonos` (
`codHistorial` int(10) UNSIGNED NOT NULL,
`bono` int(10) UNSIGNED NOT NULL,
`usuario` bigint(20) UNSIGNED NOT NULL,
`fecha` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Volcado de datos para la tabla `historialbonos`
--
INSERT INTO `historialbonos` (`codHistorial`, `bono`, `usuario`, `fecha`) VALUES
(2, 1, 1, '2020-11-20 12:52:29'),
(3, 1, 1, '2020-11-23 10:24:58'),
(4, 6, 1, '2020-11-23 10:54:54'),
(5, 1, 1, '2020-11-23 10:55:46'),
(6, 1, 1, '2020-11-23 10:58:22'),
(7, 6, 1, '2020-11-23 11:00:11'),
(8, 6, 1, '2020-11-23 11:01:15'),
(9, 6, 1, '2020-11-23 11:16:19'),
(10, 6, 1, '2020-11-23 11:30:23'),
(11, 1, 1, '2020-11-23 11:31:28'),
(12, 6, 1, '2020-11-23 11:43:28');
--
-- Índices para tablas volcadas
--
--
-- Indices de la tabla `historialbonos`
--
ALTER TABLE `historialbonos`
ADD PRIMARY KEY (`codHistorial`),
ADD KEY `historialbonos_usuario_foreign` (`usuario`),
ADD KEY `historialbonos_bono_foreign` (`bono`);
--
-- AUTO_INCREMENT de las tablas volcadas
--
--
-- AUTO_INCREMENT de la tabla `historialbonos`
--
ALTER TABLE `historialbonos`
MODIFY `codHistorial` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
--
-- Restricciones para tablas volcadas
--
--
-- Filtros para la tabla `historialbonos`
--
ALTER TABLE `historialbonos`
ADD CONSTRAINT `historialbonos_bono_foreign` FOREIGN KEY (`bono`) REFERENCES `bonos` (`codBono`),
ADD CONSTRAINT `historialbonos_usuario_foreign` FOREIGN KEY (`usuario`) REFERENCES `users` (`id`);
Hire Table
--
-- Base de datos: `bonosat`
--
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `contratan`
--
CREATE TABLE `contratan` (
`codContrato` int(10) UNSIGNED NOT NULL,
`fecha` datetime NOT NULL,
`bono` int(10) UNSIGNED NOT NULL,
`tiempoRestanteBono` int(10) UNSIGNED NOT NULL,
`bonoSolicitado` int(10) UNSIGNED NOT NULL,
`tiempoSolicitado` int(10) UNSIGNED NOT NULL,
`activo` tinyint(4) NOT NULL DEFAULT 0,
`usuario` bigint(20) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Volcado de datos para la tabla `contratan`
--
INSERT INTO `contratan` (`codContrato`, `fecha`, `bono`, `tiempoRestanteBono`, `bonoSolicitado`, `tiempoSolicitado`, `activo`, `usuario`) VALUES
(11, '2020-11-23 11:38:20', 6, 10, 1, 30, 0, 1);
--
-- Índices para tablas volcadas
--
--
-- Indices de la tabla `contratan`
--
ALTER TABLE `contratan`
ADD PRIMARY KEY (`codContrato`),
ADD KEY `contratan_usuario_foreign` (`usuario`),
ADD KEY `contratan_bono_foreign` (`bono`),
ADD KEY `contratanBonoSoli_bono` (`bonoSolicitado`);
bonuses
CREATE TABLE `bonos` (
`codBono` int(10) UNSIGNED NOT NULL,
`tipo` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`minutos` int(10) UNSIGNED NOT NULL,
`precio` double(5,2) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Volcado de datos para la tabla `bonos`
--
INSERT INTO `bonos` (`codBono`, `tipo`, `minutos`, `precio`, `created_at`, `updated_at`) VALUES
(1, '1/2H', 30, 20.00, NULL, NULL),
(2, '1H', 60, 40.00, NULL, NULL),
(3, '5H', 300, 175.00, NULL, NULL),
(4, '10H', 600, 330.00, NULL, NULL),
(5, '24H', 1200, 600.00, NULL, NULL),
(6, '10Min', 10, 9.00, NULL, NULL);
Attendance
CREATE TABLE `asistencia` (
`codAsistencia` int(10) UNSIGNED NOT NULL,
`fecha` datetime NOT NULL,
`mensaje` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`usuario` bigint(20) UNSIGNED NOT NULL,
`estado` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'pendiente',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Volcado de datos para la tabla `asistencia`
--
INSERT INTO `asistencia` (`codAsistencia`, `fecha`, `mensaje`, `usuario`, `estado`, `created_at`, `updated_at`) VALUES
(2, '2020-11-23 11:25:29', 'Prueba Histórico', 1, 'pendiente', NULL, NULL),
(3, '2020-11-23 12:32:03', 'prueba', 1, 'pendiente', NULL, NULL);
--
-- Índices para tablas volcadas
--
--
-- Indices de la tabla `asistencia`
--
ALTER TABLE `asistencia`
ADD PRIMARY KEY (`codAsistencia`),
ADD KEY `asistencia_usuario_foreign` (`usuario`);
SOLUTION
I think I've solved it by doing a subquery on the select: