I need to left join two tables with a where condition:
Table time_table
id rid start_date end_date
1 2 2017-07-01 00:00:00 2018-11-01 00:00:00
2 5 2017-01-01 00:00:00 2017-06-01 00:00:00
3 2 2018-07-01 00:00:00 2020-11-01 00:00:00
Table record_table
id name date
1 record1 2017-10-01 00:00:00
2 record2 2017-02-01 00:00:00
3 record3 2017-10-01 00:00:00
I need to get all those records which are present under given date range. In the above example, I need those records that lie under range for rid = 2 only. Hence the output for the above query needs to be:
1 record1 2017-10-01 00:00:00
3 record3 2017-10-01 00:00:00
LEFT JOINmakes sense to show the row(s) (or selected columns) fromtime_tablematchingrid = 2, even if no row inrecord_tableoverlaps with the time range. Else, if the user gets an empty result (no row), and (s)he can't tell whether there's no match intime_table(norid = 2) or inrecord_table. Of course, it would make sense to includestart_dateandend_datein the result, or you just get a row of NULL values for the latter case - which would still be significant.left joinbut it could just be some misunderstanding. Without a clarification from him I tend to put more weight on the desired output. My comment above is a call for elucidation and I should have made it clear. But congratulations for your always detailed answers.