[英]Query inner join 3 tables
I´m traying to do this query:我正在做这个查询:
SELECT DISTINCT a.codAsistencia
, a.fecha
, a.mensaje
, u.nombre
, u.id
, u.telefono
, c.tiempoRestanteBono
FROM asistencia a
JOIN users u
ON a.usuario = u.id
JOIN contratan c
ON c.usuario = u.id
WHERE a.codAsistencia = 2
I need get all this data when codAsistencia = x当 codAsistencia = x 时,我需要获取所有这些数据
phpmyadmin returned a impty result, i don´t know very well to do queries with inner join... i attach one image for you can show realations phpmyadmin 返回了一个不完整的结果,我不太清楚如何使用内部连接进行查询...我附上一张图片,您可以显示真实情况
i have to tell that i have a data en my tables, in contratan, users and asistencia... I don´t know that i´m doing wrong我必须告诉我,我的表格中有数据,在 contratan、users 和 asistencia 中...我不知道我做错了
My data in asistencia is:我在 asistencia 中的数据是:
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-11 11:27:09', 'prueba', 2, 'pendiente', NULL, NULL);
In contratan对方
CREATE TABLE `contratan` (
`codContrato` int(10) UNSIGNED NOT NULL,
`bono` int(10) UNSIGNED NOT NULL,
`tiempoRestanteBono` 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`, `bono`, `tiempoRestanteBono`, `activo`, `usuario`) VALUES
(20, 1, 30, 1, 7);
In user在用户
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
(2, '123456', 'David Serrano Alonso', 'daviserraalonso@hotmail.com', NULL, '$2y$10$/BfMjo1J8W.UFtQAGTUX9O0Ae9RilA5JFipu0ZVnLWrwDSIQ65NBS', NULL, 'tecnico', NULL, NULL, '2020-11-08 10:59:29', '2020-11-08 10:59:29'),
(3, '45921676z', 'David Serrano Alonso', 'daviserraalonso@pepemail.com', NULL, '$2y$10$wLEOohpZrxbAcFcKC07x5u5gNeA/zeJZwUWNLoec5FsrUfQ5P.9Nu', NULL, 'usuario', 652138927, NULL, '2020-11-11 07:56:12', '2020-11-11 07:56:12'),
(7, '45921676z', 'David Serrano Alonso', 'daviserraalonso@gmail.com', '2020-11-11 08:21:19', '$2y$10$wT2YvsBlypLD.FjADnP0s.vk2liOq/tFM.vHeCGXO3jyGiBReyzsm', NULL, 'usuario', 652138927, NULL, '2020-11-11 08:20:46', '2020-11-11 08:21:19');
Thank you very much for help非常感谢您的帮助
I think you want left join
s:我想你想要
left join
s:
SELECT distinct a.codAsistencia, a.fecha, a.mensaje,
u.nombre, u.id, u.telefono, ct.tiempoRestanteBono
FROM asistencia a LEFT JOIN
users u
ON a.usuario = u.id LEFT JOIN
contratan ct
ON ct.usuario = u.id
WHERE a.codAsistencia = 2;
This will keep all matching rows in asistencia
, even if there are no matching rows in the other tables.这将保留
asistencia
所有匹配行,即使其他表中没有匹配的行。
Note that I also added table aliases so the query is simpler to write and to read.请注意,我还添加了表别名,因此查询更易于编写和读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.