我有以下表结构:
表名:有用
id (autoincremetn) | acc_id | start_date | end_date
-------------------------------------------------------
1 | 175 | 2015-05-26 | 2015-05-31 |
-------------------------------------------------------
2 | 175 | 2015-07-01 | 2015-07-07 |
-------------------------------------------------------
它用于定义日期范围可用性,例如. start_date和end_date之间的所有日期都不可用于给定的acc_id.
基于用户输入我正在关闭不同的范围,但我想抛出一个错误,如果用户试图关闭(提交)一个已开始的范围或在已存在的范围内某处的end_date(对于提交的acc_id)在DB中.
在这个例子中,start_date:2015-05-30 end_date:2015-06-04将是一个很好的失败候选人.
这几乎解释了如何分两步完成,两个查询之间有一些PHP逻辑.
但我想知道是否可以在一个插入语句中完成.
我最终会检查受成功或失败影响的行(子问题:是否有更方便的方法来检查除了日期重叠之外是否因为某些其他原因而失败?)
编辑:
为了回应Petr的评论,我将进一步说明验证:
any kind of overlapping should be avoided, even the one embracing the
whole range or finding itself inside the existing range. Also, if
start or end dates equal the existing start or end dates it must be
considered an overlap. Sometimes certain acc_id will already have more
than one rang in the table so the validation should be done against
all entries with a given acc_id.