Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 日期时间比较?_Mysql_Sql_Datetime_Date Arithmetic - Fatal编程技术网

Mysql 日期时间比较?

Mysql 日期时间比较?,mysql,sql,datetime,date-arithmetic,Mysql,Sql,Datetime,Date Arithmetic,我试图在MySQL中从使用UNIX时间戳切换到DATETIME列,但在找到正确的方法来比较日期时遇到了一些困难 我尝试使用+和-操作符来比较两个日期时间,结果对我来说没有任何意义 例如: 1. SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() - INTERVAL 1 HOUR 输出 2014-07-06 19:19:13 | 2014-07-06 18:19:13 这些 两个输出均10000。这个数字对我来说没有意义,但它变得更加混乱,因为: SELECT U

我试图在MySQL中从使用UNIX时间戳切换到
DATETIME
列,但在找到正确的方法来比较日期时遇到了一些困难

我尝试使用
+
-
操作符来比较两个
日期时间
,结果对我来说没有任何意义

例如:

1.

SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() - INTERVAL 1 HOUR
输出

2014-07-06 19:19:13 | 2014-07-06 18:19:13
这些

两个输出均
10000
。这个数字对我来说没有意义,但它变得更加混乱,因为:

SELECT UTC_TIMESTAMP()-DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 SECOND)
输出
1
。为什么呢?这个数字代表什么

2.

SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() - INTERVAL 1 HOUR
显示
DATE\u ADD()
DATE\u SUB()
可用于从日期中添加和减去间隔,但我在手册中没有看到与大于和小于运算符对应的任何函数,因此如何检查当前日期是否大于其他日期

我尝试使用
操作符,它们似乎可以工作,但我在手册中似乎找不到任何关于这方面的内容,我想确保可以像这样使用这些操作符:

SELECT UTC_TIMESTAMP() > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR)


任何人都可以在MySQL中解密
DATETIME
比较吗?

引用有关
UTC\u TIMESTAMP()
的文档:

以“YYYY-MM-DD”中的值返回当前UTC日期和时间 HH:MM:SS'或YYYYMMDDHHMMSS格式,取决于函数 在字符串或数字上下文中使用


因为值是在数字的上下文中使用的,所以它被视为一个数字,这是您看到的行为。

我自己刚刚发现了这一点,但这里有一个简单的示例向您展示了它是如何工作的

@GordonLinoff的答案既不在这里也不在那里,因为您的问题并不是关于从
utc\u timestamp()
返回的格式。您真正要问的是,当您在时间戳上使用数字操作数
+
-
时,MySQL返回什么格式

我倾向于同意你的观点,文档在这个主题上有点模糊。但这就是我发现的。您可以自己构建此视图,以更简单的方式查看示例

create view cbhview as
select utc_timestamp() as nowtime,
       utc_timestamp() - interval 1 hour as thentime,
       date_sub(utc_timestamp(),INTERVAL 1 HOUR) as thentime2,
       date_sub(utc_timestamp(),INTERVAL 1 SECOND) as justthentime;

select nowtime, thentime, thentime2, justthentime,
       nowtime-thentime,
       nowtime-justthentime,
       thentime-thentime2
from   cbhview;

+---------------------+---------------------+---------------------+---------------------+------------------+----------------------+--------------------+
| nowtime             | thentime            | thentime2           | justthentime        | nowtime-thentime | nowtime-justthentime | thentime-thentime2 |
+---------------------+---------------------+---------------------+---------------------+------------------+----------------------+--------------------+
| 2014-07-06 20:22:58 | 2014-07-06 19:22:58 | 2014-07-06 19:22:58 | 2014-07-06 20:22:57 |            10000 |                    1 |                  0 |
+---------------------+---------------------+---------------------+---------------------+------------------+----------------------+--------------------+
1 row in set (0.00 sec)
  • 100000
    表示
    1h 00分钟00秒
  • 1
    表示
    1秒
  • 0
    表示两者之间没有区别

简言之,除非您确切地知道自己在做什么以及想要实现什么,否则不要在
date
timestamp
数据类型上使用数字操作数。请继续使用专门设计的函数
date\u add()
date\u sub()

谢谢,这会让您轻松很多。因此,对日期时间值使用
+
-
并不能产生理想的结果,但是使用大于/小于比较又如何呢?使用
是检查一个日期时间值是否大于/小于另一个日期时间值的适当方法吗?@Nate是的,当然可以使用
@Nate我刚才看到了你要做的。是的,您也可以在
select
语句中放置逻辑比较器,但这不会过滤(就像在语句的
where
部分中那样)。所以再一次,小心你知道你想要什么。在
select
语句中,逻辑比较器给出
true/false
(实际上是
1/0
)响应<代码>选择现在时间