简体   繁体   English

查询中的日期比较:正确的逻辑但错误的结果

[英]Date comparison in query: Correct logic but wrong result

I have this very simple query.我有这个非常简单的查询。 But it's returning wrong result.但它返回错误的结果。

select * from `table_a` where date(`schedule`) > '2021-11-13 06:31:00'

In my table, I have these rows在我的表中,我有这些行

table_a
+---------------------+
|      schedule       |
+---------------------+
| 2021-11-13 08:59:00 |
+---------------------+
| 2021-11-13 08:59:00 |
+---------------------+
| 2021-11-13 08:59:00 |
+---------------------+

Technically, this query should return all rows.从技术上讲,此查询应返回所有行。 But 0 rows is returned.但返回 0 行。 I tried changing the operation to < and it returned all rows (which is the opposite) .我尝试将操作更改为<并返回所有行(相反)。 Did I miss something here?我在这里错过了什么吗?

note: schedule datatype is DATETIME.注意: schedule数据类型是 DATETIME。

Your schedule column is datetime, with a time component, so remove the cast to date:您的schedule列是日期时间,带有时间组件,因此删除迄今为止的演员表:

SELECT * FROM table_a WHERE schedule > '2021-11-13 06:31:00';

What was happening is that your 3 records were all being casted to 2021-11-13 at midnight, which is the same as 2021-11-13 00:00:00 .发生的事情是您的 3 条记录都在午夜2021-11-132021-11-13 ,这与2021-11-13 00:00:00相同。 It should be clear that all of the 3 records occurred past midnight on this date.应该清楚的是,所有 3 条记录都发生在该日期的午夜之后。

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

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