[英]Mysql Delete between two dates
我想删除两个日期之间的详细信息
我的代码如下
Delete from emp_leave_details
where emp_id='$id'
and from_date BETWEEN '$f_date' AND '$t_date'
问题是它删除了$f_date
和$t_date
之间的所有记录,但没有删除$t_date
的记录。我也想从$t_date
删除记录。
Delete from emp_leave_details
where emp_id='$id'
and DATE(from_date) BETWEEN DATE('$f_date') AND DATE('$t_date')
您的查询等于from_date >= '$f_date' AND from_date <= '$t_date'
,这意味着它将忽略最后一天的所有内容('2013-09-05' < '2013-09-05 00:00:01')
。 我上面的给出了('2013-09-05' <= '2013-09-05')
代替。
由于BETWEEN
使用<=
和>=
因此DATE()
仅仅去除小时,分钟和秒的值,并为您提供预期的结果。
尝试这个...
Delete from emp_leave_details
Where emp_id='$id'
AND from_data > '$f_date'
AND from_date <= '$t_date';
如果您有一个datetime
列,则使用日期将不够,因为2013-09-25 12:00:00
大于 2013-09-25
你可以吗
Delete from emp_leave_details
Where emp_id = '$id'
and from_date >= '$f_date'
and from_date < '$t_date'
并将'$t_date'
设置为1天后。
我看不到为什么还要在查询中包含ID,但是直接回答问题时,您应该
Delete from emp_leave_details
Where emp_id='$id'
and from_date > '$f_date'
and from_date <= '$t_date'
mysql_query("Delete from emp_leave_details Where emp_id='$id' and (from_date BETWEEN '$f_date' AND '$t_date'");
问题是对于$ t_date,它始于00:00:00。 那是一天的开始。 白天的一切都不包括在内。 如果要包括$ t_date的记录,则应在其中添加1 。
$t_date = $t_date + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.