I have two tables, hours and days, hours store the hours a profesional has available in a day. Days, store the the reserved hours,
table_hours
id | begin | end | profesional
1 | 09:00 | 10:00 | 1
2 | 10:00 | 11:00 | 1
3 | 11:00 | 12:00 | 1
4 | 13:00 | 14:00 | 1
5 | 14:00 | 15:00 | 1
6 | 09:30 | 10:30 | 2
7 | 13:00 | 14:30 | 2
8 | 14:30 | 15:30 | 2
table_days
id | hour_id | day
1 | 1 |20151201
2 | 3 |20151201
3 | 6 |20151201
4 | 2 |20151205
5 | 7 |20151205
6 | 8 |20151205
I, tried the following query, but do not worked.
SELECT *, (SELECT day from days where hours.id = days.hours_id and day = '20151201') as 'day'
FROM hours
where profesional = 1
As result I would like to have all results from profesional and a date just when match as bellow
table_result
id | begin | end | profesional | day
1 | 09:00 | 10:00 | 1 |20151201
2 | 10:00 | 11:00 | 1 |NULL
3 | 11:00 | 12:00 | 1 |20151201
4 | 13:00 | 14:00 | 1 |NULL
5 | 14:00 | 15:00 | 1 |NULL