Answer the question
In order to leave comments, you need to log in
SQL returns null on left join?
Hello! There are 2 tables users and invitations, the table of invitations stores who invited a certain user, if he came himself, then NULL. And I try to look for a certain period of time.
But the problem is with a certain period of time. always returns null when outputting data.
That is, I have displayed users for the entire period of time, I don’t know what to think of.
Structure:
--
-- Структура таблицы `inviteds`
--
CREATE TABLE `inviteds` (
`id` int(10) UNSIGNED NOT NULL,
`user_id` int(11) NOT NULL,
`referer` int(11) DEFAULT NULL,
`first` int(11) NOT NULL DEFAULT '0',
`second` int(11) NOT NULL DEFAULT '0',
`third` int(11) NOT NULL DEFAULT '0',
`first_earn` double NOT NULL DEFAULT '0',
`first_gth_earn` double NOT NULL DEFAULT '0',
`second_earn` double NOT NULL DEFAULT '0',
`second_gth_earn` double NOT NULL DEFAULT '0',
`third_earn` double NOT NULL DEFAULT '0',
`third_gth_earn` double NOT NULL DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
--
-- Структура таблицы `users`
--
CREATE TABLE `users` (
`id` int(10) UNSIGNED NOT NULL,
`name` varchar(190) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(190) COLLATE utf8mb4_unicode_ci NOT NULL,
`token` varchar(190) COLLATE utf8mb4_unicode_ci NOT NULL,
`balance` double NOT NULL DEFAULT '0',
`tokens` decimal(8,2) NOT NULL DEFAULT '0.00',
`role` int(11) NOT NULL DEFAULT '0',
`password` varchar(190) COLLATE utf8mb4_unicode_ci NOT NULL,
`cheater` int(11) NOT NULL DEFAULT '0',
`banned` int(11) DEFAULT NULL,
`active` int(11) NOT NULL DEFAULT '0',
`ip` varchar(190) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`code` varchar(190) COLLATE utf8mb4_unicode_ci 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;
SELECT *
FROM `inviteds`
LEFT JOIN `users` AS `refererJoin`
ON `referer` = `refererJoin`.`id`
AND `refererJoin`.`active` = 1
AND `refererJoin`.`banned` = 0
LEFT JOIN `users` AS `userJoin`
ON `user_id` = `userJoin`.`id`
AND `userJoin`.`active` = 1
AND `userJoin`.`banned` = 0
AND `userJoin`.`created_at` > '2020-02-01 00:00:00'
AND `userJoin`.`created_at` < '2020-03-01 00:00:00'
WHERE `referer` IS NOT NULL
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question