Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Join 3 MySQL tables

I have the following 3 MySQL tables.
I am storing bookings in the bookings table & the event dates and times in separate MySQL tables.

I want to have a MySQL query to list available times for a specific date.

So if I enter date of value 1 it’ll show no times available but if I enter 2 it’ll output 1 | 9:00.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

INSERT INTO `bookings` (`id`, `email`, `date_requested`, `time_requested`) VALUES
(1, 'test@test.com',    '1',    '1'),
(2, 'test2@test.com',   '1',    '2'),
(3, 'test3@test.com',   '2',    '2');

INSERT INTO `bookings_dates` (`id`, `date`) VALUES
(1, '2022-11-05'),
(2, '2022-11-06'),
(3, '2022-11-07');

INSERT INTO `bookings_times` (`id`, `time`) VALUES
(1, '9:00'),
(2, '9:15');

>Solution :

You can use a NOT IN condition.

SELECT *
FROM bookings_times
WHERE id NOT IN (
    SELECT time_requested
    FROM bookings
    WHERE date_requested = (
        SELECT id 
        FROM bookings_dates 
        WHERE date = '2022-11-05'
    )
)

DEMO

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading