dsjklb0205 2013-01-03 02:25
浏览 41
已采纳

MySQL之间的日期范围

I have a scenario where I need to pull up delivery dates based on a table below (Example)

job_id | delivery_date
1      | 2013-01-12
2      | 2013-01-25
3      | 2013-02-15

What I'm trying to do is show the user all the delivery dates that start with the earliest (in this case it would be 2013-01-12) and add an another 21 days to that. Basically, the output I would expect it to show of course, the earliest date being the starting date 2013-01-12 and 2013-01-25. The dates past the February date are of no importance since they're not in my 21 date range. If it were a 5 day range, for example, then of course 2013-01-25 would not be included and only the earliest date would appear.

Here is main SQL clause I have which only shows jobs starting this year forward:

SELECT date, delivery_date 
FROM `job_sheet` 
WHERE print_status IS NULL 
    AND job_sheet.date>'2013-01-01'

Is it possible to accomplish this with 1 SQL query, or must I go with a mix of PHP as well?

  • 写回答

3条回答 默认 最新

  • donglin9717 2013-01-03 02:31
    关注

    You can use the following:

    select *
    from job_sheet
    where print_status IS NULL
      and delivery_date >= (select min(delivery_date)
                            from job_sheet)
      and delivery_date <= (select date_add(min(delivery_date), interval 21 day)
                            from job_sheet)
    

    See SQL Fiddle with Demo

    If you are worried about the dates not being correct, if you use a query then it might be best to pass in the start date to your query, then add 21 days to get the end date. Similar to this:

    set @a='2013-01-01';
    
    select *
    from job_sheet
    where delivery_date >= @a
      and delivery_date <= date_add(@a, interval 21 day)
    

    See SQL Fiddle with Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程