dtll2016 2011-09-16 08:48
浏览 38
已采纳

如果在输入的日期范围内不存在,请获取最近的日期

Im doing a backend now, and I'm trying to make a list of number of rooms by date range.

DB Structure

id room_type_id room_count date_applied
1  1            2          2011-09-01
1  1            3          2011-09-05
1  1            1          2011-09-06

In the HTML:

If the user inputted From: 2011-Sept-01 To: 2011-Sept-06. This would be the display. (This is already OK)

Date          Room Count
2011-Sept-1   2
2011-Sept-2   2
2011-Sept-3   2
2011-Sept-4   2
2011-Sept-5   3
2011-Sept-6   1

And my problem now is that, when the user inputted into From and To is already out of range from the data stored in the database. Sample: From: 2011-09-07 To: 2011-09-09

The Output should be displayed in the HTML should be look like this.

Date          Room Count
2011-Sept-7   1
2011-Sept-8   1
2011-Sept-9   1

It will get the closest/last data in the database. For this sample it would be 2011-09-06

Thanks for any help.

  • 写回答

1条回答 默认 最新

  • douya2982 2011-09-16 08:56
    关注

    For that you may need to run a sub query finding the immediate lowest date? and then start from that date to the end date.

    Forexample.

    SELECT * FROM tbl WHERE date BETWEEN (SELECT date FROM tbl WHERE date < lower_range LIMIT 1 ) AND upper_range
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元