MySQL : How to select record where begin date and end date between two dates of mysql

标签 mysql sql select

我需要选择入住日期和退房日期在指定日期范围之间的房价。这些费率根据其条件单独命名。房费取决于所选日期。这是我的代码:

rate_eb

rate_name     rate_starts     rate_ends     rate_discount     rate_min_stay
---------------------------------------------------------------------------
Low Season    2013-05-01      2013-10-31    20                3            
High Season1  2013-11-01      2013-12-19    20                4
High Season2  2013-02-21      2013-04-31    20                4
Peak Season   2013-12-20      2014-02-20    20                5            

条件是:

  1. 预订必须在 rate_startsrate_ends 之间。
  2. 总停留天数必须大于或等于 rate_min_stay
  3. rate_discount 是另一个表中主费率的折扣百分比。假设主费率为 100,则此预订将应用 80 的费率。

现在,我想从带有日期范围的 rate_db 中获取这些数据 - 尤其是 rate_discount。这是我的 mySQL:

select rate_discount 
from rate_eb 
where rate_min_stay<='4' 
and reb_starts>='2013-06-19' 
and reb_ends<='2013-06-23'

来自上面的代码。我预计 淡季 rate_discount=20 但所有小于或等于 4 的费率都被选中。

现在,请给我建议解决方案。我如何重写我的代码以访问 rate_starts 和 rate_ends 之间的 rate_discount。

最佳答案

我假设访问者可以输入一个时期,而不仅仅是一个日期。如果那个时期的开始日期是淡季而结束日期是旺季会怎样?那么您希望看到哪个汇率?

select rate_discount 
from rate_eb 
where rate_min_stay <= abs( datediff( reb_date2, reb_date1 ) )
and reb_date1 between rate_starts and rate_ends
and reb_date2 between rate_starts and rate_ends

我假设 reb_date1 是访问者输入的开始日期,reb_date2 是结束日期。

关于MySQL : How to select record where begin date and end date between two dates of mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15321428/

相关文章:

sql - Oracle ALTER 语句中可以使用子查询吗?

mysql - 如何在特定条件下自连接表

mysql - 可以以任何方式选择子选择中的别名

mysql - SQL语句创建许多连接并挂起数据库

mysql - 用户 'ODBC' @'localhost' 的访问被拒绝

php - 表实际上不存在,但从该表查询返回数据

jquery - 设置选择列表中要选择的项目

mysql - Laravel 5 选择查询将我的链接变成奇怪的格式

php - 如何修复将 php 表单插入 mysql 数据库时出现错误

mysql防止负数