简体   繁体   English

mysql中日期比较(到时间戳列)的错误结果

[英]Incorrect result for date comparison (to a timestamp column) in mysql

I've been having some 'strange' results while comparing dates. 在比较日期时,我一直有一些“奇怪”的结果。
table1 has two rows with TIMESTAMPS values 2009-08-26 23:39:56 and 2009-08-27 00:01:42 table1有两行的TIMESTAMPS值分别为2009-08-26 23:39:562009-08-27 00:01:42 2009-08-26 23:39:56
When I make this query: 当我进行此查询时:

select * from table1 c
INNER JOIN table2 r ON r.table1_id = c.id
WHERE DATE(c.authorization_date) = '2009-08-26'

it returns both rows (when it only should have returned one). 它返回两行(当它仅应返回一个时)。

For added weirdness the rows in the returned resultSet have tha same value: 2009-08-26 23:39:56 为了增加怪异,返回的resultSet中的行具有相同的值: 2009-08-26 23:39:56

But If I make this query: 但是,如果我进行以下查询:

SELECT DATE(authorization_date) FROM table1

It correctly returns two rows with values 2009-08-26 and 2009-08-27 它正确返回两行,其值分别为2009-08-262009-08-27

So, here comes my questions. 所以,这是我的问题。 How could I make the comparison so the correct result is returned, what am i doing wrong? 我如何进行比较,以便返回正确的结果,我在做什么错? Could be related to the inner join? 可能与内部联接有关吗?

I suspected: 我怀疑:

  • some timezone discrepency, but you seem to have accounted for that 某些时区差异,但您似乎已经对此做出了解释

  • some other data that is confusing the problem... is there something else that could be interfering? 其他一些令人困惑的数据...还有其他可能干扰的东西吗?

Wish I could recreate this and help. 希望我能重做并提供帮助。 Here's my setup code. 这是我的设置代码。 What am I missing? 我想念什么?

mysql> create table table1 (id integer primary key auto_increment, authorization_date TIMESTAMP);
mysql> insert into table1 values (1, '2009-08-26 23:39:56');
mysql> insert into table1 values (2, '2009-08-27 00:01:42');
mysql> create table table2 (table1_id integer);
mysql> insert into table2 values (1);
mysql> insert into table2 values (2);


    mysql> SELECT DATE(authorization_date) FROM c;
    +--------------------------+
    | DATE(authorization_date) |
    +--------------------------+
    | 2009-08-26               | 
    | 2009-08-27               | 
    +--------------------------+

    mysql> select * from table1 c INNER JOIN table2 r ON r.table1_id = c.id WHERE DATE(c.authorization_date) = '2009-08-26';
    +----+---------------------+-----------+
    | id | authorization_date  | table1_id |
    +----+---------------------+-----------+
    |  1 | 2009-08-26 23:39:56 |         1 | 
    +----+---------------------+-----------+
    1 row in set (0.00 sec)

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

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