I would like to perform select on joined tables and retrieve those rows, where at least one of four column is between two given dates.
Currently I have this:
SELECT
t1.Id,
t1.DateCr AS t1DateCr,
t1.DateUpd AS t1DateUpd,
t2.DateCr AS t2.DateCr,
t2.DateUpd AS t2DateUpd
FROM
Table1 AS t1,
Table2 AS t2,
WHERE
t1.Id = t2.t1Id
AND (t1.DateCr BETWEEN '2018-10-29' AND '2018-11-04'
OR t1.DateUpd BETWEEN '2018-10-29' AND '2018-11-04'
OR t2.DateCr BETWEEN '2018-10-29' AND '2018-11-04'
OR t2.DateUpd BETWEEN '2018-10-29' AND '2018-11-04')
GROUP BY t1.Id
Can this be written in more elegant way? without repeating the BETWEEN
condition several times?
I can not use GREATEST
and LEAST
, because I dont want to select rows, where dates are bigger and smaller than given range, but not inside it.
And (t1.DateCr,t1.DateUpd,t2.DateCr,t2.DateUpd) BETWEEN '2018-10-29' AND '2018-11-04'
does not work.