简体   繁体   English

在MySQL中过滤表,其中两个时间戳列之间存在差异

[英]Filtering table in MySQL with difference between two timestamp columns

I have a table in MySQL which includes the datetime columns open_time and close_time . 我在MySQL中有一个表,其中包含datetime列open_timeclose_time

How can I select only rows in which close_time is at least one minute greater than open_time ? 如何只选择close_timeopen_time大至少一分钟的open_time

WHERE TIMESTAMPDIFF(SECOND,close_time,open_time) > 60

更多信息: http : //www.w3resource.com/mysql/date-and-time-functions/mysql-timestampdiff-function.php

You could use TIMESTAMPDIFF(unit, datetime_expr1, datetime_expr2) and something like 您可以使用TIMESTAMPDIFF(unit, datetime_expr1, datetime_expr2)和类似的东西

select * from MyTab T where TIMESTAMPDIFF(MINUTE,open_date,close_date) > 1

From docs 来自文档

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2) TIMESTAMPDIFF(单元,datetime_expr1,datetime_expr2)

Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. 返回datetime_expr2-datetime_expr1,其中datetime_expr1和datetime_expr2是日期或日期时间表达式。 One expression may be a date and the other a datetime; 一个表达式可以是日期,另一个可以是日期时间。 a date value is treated as a datetime having the time part '00:00:00' where necessary. 在必要时,将日期值视为具有时间部分“ 00:00:00”的日期时间。 The unit for the result (an integer) is given by the unit argument. 结果的单位(整数)由unit参数指定。 The legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function. 单位的合法值与TIMESTAMPADD()函数的说明中列出的值相同。

mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01'); mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01'); -> 3 -> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01'); mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01'); -> -1 -> -1
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55'); mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55'); -> 128885 -> 128885

Note The order of the date or datetime arguments for this function is the opposite of that used with the TIMESTAMP() function when invoked with 2 arguments. 注意此函数的日期或日期时间参数的顺序与使用2个参数调用时TIMESTAMP()函数所使用的顺序相反。

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

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