当前位置: 动力学知识库 > 问答 > 编程问答 >

Check if two date ranges overlap in Rails

问题描述:

Events have start_at and end_at fields

How do I check if there is any overlap between two events using only database queries?

In other words, is something like

(Event1.start_at BETWEEN Event2.start_at AND Event2.end_at) OR (Event1.end_at BETWEEN Event2.start_at AND Event2.end_at)

possible with .where function?

Thanks!

网友答案:

If you're using MySQL, it also has a BETWEEN ... AND (which is just a shortcut for (min <= expr AND expr <= max)).

分享给朋友:
您可能感兴趣的文章:
随机阅读: