duanpo8329 2012-12-06 11:09
浏览 14
已采纳

查找日期范围与指定日期范围相交的行

I have a table leave_applications with two columns from date and to date. i need to get those rows from the table which have leave dates falling in a given month, means the rows in which at least one of the dates between from date and to date falls in the given month.

Hence I need a query that will get me rows that have intersection of the two sets of dates (i.e. from_date and to_date) and all dates of that month.

  • 写回答

2条回答 默认 最新

  • douyan1882 2012-12-06 11:34
    关注

    I wrote about these queries some time ago. Here is the solution:

    -- 2.2) select date ranges that overlap [d1, d2] (d2 and end_date are inclusive)
    SELECT * FROM <table> WHERE @d2 >= start_date AND end_date >= @d1
    

    So if you want to check the leaves for Dec 2012, you should write:

    SELECT * FROM leave_applications
    WHERE '2012-12-31' >= from_date AND to_date >= '2012-12-01'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题