使用mysql比较记录小于或大于记录

时间:2015-02-23 12:53:37

标签: php mysql datetime

我的问题是比较<=>=之间的记录 我的表包含列ID,名称,地址,堆栈,StartDateTime,EndDateTime。 我想在StartDateTimeEndDateTime之间获取记录。 日期时间格式为0000-00-00 00:00:00

mysql-> Select * from Table_nm where  StartDateTime>= $DATETIME and EndDateTime<= $DATETIME1;

所以,我的问题是我如何比较记录,

1. $DATETIME= 2015-02-03 10:00:00 and $DATETIME1 =2015-02-06 20:00:00 
2. $DATETIME= 2015-02-03 05:00:00 and $DATETIME1 =2015-02-06 13:00:00
3. $DATETIME= 2015-02-03 10:00:00 and $DATETIME1 =2015-02-06 11:00:00
明智的记录包含日期时间, 所以,给我下面显示的过滤结果。

1.If I want to filter record where StartDateTime>=2015-02-03 00:00:00 and EndDateTime<= 2015-02-06 00:00:00.
2.If I want to filter record where StartDateTime>=2015-02-03 09:00:00 and EndDateTime<= 2015-02-06 12:00:00.
3.If I want to filter record where StartDateTime>=2015-02-03 01:00:00 and EndDateTime<= 2015-02-06 22:00:00.

以及此过滤器如何适用于DateTime >=<=,请帮忙!

1 个答案:

答案 0 :(得分:0)

我使用TIMESTAMPDIFF

在您的示例中,它将类似于:

mysql-> Select * from Table_nm
        where TIMESTAMPDIFF(SECOND, `StartDateTime`, '$DATETIME')>= 0
          and TIMESTAMPDIFF(SECOND, `EndDateTime`, '$DATETIME1')<= 0;