简体   繁体   English

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

[英]Comparing record between less than or greater than record using mysql

My question is compare the record between <= and >= my table contain columns ID,Name,Address,Stacks,StartDateTime,EndDateTime. 我的问题是比较<=>=之间的记录>=我的表包含列ID,名称,地址,堆栈,StartDateTime,EndDateTime。 I want to fetch record between StartDateTime and EndDateTime . 我想在StartDateTimeEndDateTime之间获取记录。 DateTime Format is 0000-00-00 00:00:00 DateTime格式为0000-00-00 00:00:00

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

So,my question is How I compare records that, 所以,我的问题是如何比较记录,

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

like wise records contains datetime, so ,Give me results for filter shown below. 像明智的记录包含datetime,所以,给我下面显示的过滤结果。

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.

and How this filter for works for DateTime >= and <= , Please help! 以及此过滤器如何适用于DateTime >=<= ,请帮忙!

I use TIMESTAMPDIFF 我使用TIMESTAMPDIFF

In your example, it would be something like: 在您的示例中,它将类似于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM