简体   繁体   English

SQL如何将DateTime与分钟进行比较

[英]SQL How to compare DateTime to minutes

I have two different set of DateTime in database. 我在数据库中有两组不同的DateTime。 How do i select time to minutes. 我如何选择时间到分钟。 I want to select User if his InTime and OutTime is same up to minutes and NOT seconds. 如果他的InTime和OutTime相同(最多几分钟而不是几秒钟),我想选择User。 So in this case it should select User1 and User2. 因此,在这种情况下,应选择User1和User2。 . The seconds are ignored. 秒将被忽略。 How to create such SQL query? 如何创建这样的SQL查询?

Table: DailyTimeAttendance

User: 1
InTime: 2018-01-01 05:01:07
OutTime: 2018-01-01 05:01:05

User: 2
InTime: 2018-01-01 02:02:04
OutTime: 2018-01-01 02:02:09

User: 3
InTime: 2018-01-01 09:04:05
OutTime: 2018-01-01 09:06:10

Expected Output SQL: 
User1 InTime 2018-01-01 00:01 compared to OutTime 2018-01-01 00:01 is TRUE

You can use DATEDIFF functions if you are using the Microsoft SQL Server, This function returns the count (as a signed integer value) of the specified datepart boundaries crossed between the specified startdate and enddate. 如果使用的是Microsoft SQL Server,则可以使用DATEDIFF函数。此函数返回在指定开始日期和结束日期之间交叉的指定日期部分边界的计数(作为有符号整数值)。 EXAMPLE (if are in same minute) 示例(如果在同一分钟内)

SELECT * FROM DailyTimeAttendance 
WHERE DATEDIFF(MINUTE,InTime,OutTime)=0

In mysql: 在mysql中:

select User, Intime, OutTime, 
case when date_format(Intime, '%Y-%m-%d %k:%i') = date_format(OutTime, '%Y-%m-%d %k:%i')
then 'TRUE' else 'FALSE' end
from DailyTimeAttendance

This should serve your purpose. 这应该达到您的目的。

select * from 
DailyTimeAttendance where
 CONVERT(DATE_FORMAT(InTime,'%Y-%m-%d-%H:%i:00'), DATETIME) = 
 CONVERT(DATE_FORMAT(OutTime,'%Y-%m-%d-%H:%i:00'), DATETIME)

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

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