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


Comparing record between less than or greater than record using mysql

我的问题是比较<=>=之间的记录我的表包含ID、Name、Address、Stacks、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 >=<=,请帮助!

我使用TIMESTAMPDIFF

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

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