检查2个日期之间的差异是否不超过2天

时间:2015-06-22 20:31:03

标签: mysql sql

我在mySQL中完成了我的PHP脚本检查,我选择符合条件的条目,我需要增强这个:

outDate

inDate必须晚于$sql = " SELECT id FROM inputs_outputs WHERE objectId=".sqltext($data["objectId"])." AND nfcTagId= ".sqltext($data["nfcTagId"])." AND nfcText = ".sqltext($data["nfcText"])." AND inDate < ".sqltext(date("Y-m-d H:i:s", strtotime($data["outDate"])))." AND inDate !='0000-00-00 00:00:00' AND outDate = '0000-00-00 00:00:00' ORDER BY inDate DESC LIMIT 1"; ,但最迟应超过2天。查询现在:

{{1}}

1 个答案:

答案 0 :(得分:1)

datediff返回两个日期之间的天数,因此您可以使用以下where子句来确保outDate比inDate大不超过2天

where outDate > inDate AND datediff(outDate,inDate) <= 2