对于这样的比较,我想将日期时间转换为“零时以来的月份”。你,你可以用算术进行计算。
您的查询,这看起来像:
select t.*, year(compdate), month(compdate)
from t cross join
(select date('2013-07-01') as compdate) const
where year(compdate)*12+month(compdate) between year(dtBeg)*12 + month(dtBeg) and
year(dtEnd)*12 + month(dtEnd);
在这里,我已经把compdate
在子查询。这样,如果您想检查多个月,则可以将表添加到表中:
select t.*, year(compdate), month(compdate)
from t cross join
(select date('2013-07-01') as compdate union all
select date('2013-08-01')
) const
where year(compdate)*12+month(compdate) between year(dtBeg)*12 + month(dtBeg) and
year(dtEnd)*12 + month(dtEnd);
此表单可以在许多SQL方言中使用。你可以使用date_format()
做类似于MySQL特定功能的操作。
select t.*, year(compdate), month(compdate)
from t cross join
(select '2013-07' as compdate union all
select '2013-08'
) const
where compdate between date_format(dtBeg, '%Y-%m') and date_format(dtEnd, '%Y-%m)
谢谢你,你的解决方案完美。现在我必须明白为什么:)。 – SirKometa