CREATE TABLE table
(`id` int, `date` date, `time` time);
INSERT INTO table
(`id`, `date`, `time`)
VALUES
(1, '2022-05-22', 14:00:00),
(2, '2022-05-23', 07:35:00),
(4, '2022-05-23', 14:00:00);
Expected Output:
date | time |
---|---|
2022-05-22 | 14:00:00 |
2022-05-22 | NULL |
2022-05-23 | 07:35:00 |
2022-05-23 | 14:00:00 |
As you notice, there’s no entry for 07:35 at date 2022-05-22.
I have tried join on single table and CTE also but nothing works.
I’m using PHP with MySQL
Thanks in advance.
2
Answers
Solution given by @Tim Biegeleisen is working just fine but unfortunately my server doesn't suppoert CTE, so who has little knowledge of CTE or have lower version of mysql can try this solution...
A calendar table approach might be what you are looking for. Consider the following select query which however generates the exact output you want.