I have a query that selects from one table based on rows in a related table not existing for a particular date. Right now, I call this query several times once with each date I'm interested in. But for performance reasons, I'd like one round trip to the database. I want to expand this query to work over a date range and return all rows in the range where there are not related rows in the other table for each date (ie UNION of results of current query for each date in range). The results must include the date itself in each row. My current query is something like this:
SELECT
t1.id,
t1.field1,
t1.field2,
'2016-03-17' AS nightof_date,
1 as marker
FROM
t1
LEFT JOIN t2 ON (
(t2.a_date = '2016-03-17')
AND (t2.t1_id=t1.id)
AND (some conditions...))
WHERE
(t2.id is NULL)
AND (some other conditions...)
GROUP BY
t1.field3;
Here's a fiddle of a single query: http://sqlfiddle.com/#!9/553d49/6/0
And here's what I'm trying to achieve as a result for a start date 2016-03-17 and end-date 2016-03-20: http://sqlfiddle.com/#!9/553d49/6/0
(I suppose I could programmatically generate a monster union query like this but I was hoping for something a bit more elegant where I could just use the start and end dates)
Is there a way to do this without using the NUMBERS or similar trick (ie. I'm hoping not to have to generate a seperate table)?