Home  >  Q&A  >  body text

mysql中datetime列如何做筛选?

1,数据库中有个数据的创建时间的列 F1,类型是 datetime 类型
2,现在准备做个根据给定时间来筛选数据的功能
3,现在的SQL代码如下:

select * from TABLE where F1 > '2017-01-01 12:23:33'

上面的 > 可以是 < 或者 <=,重点是没用 between and,根据观察发现这么去筛的时候出来的数据有可能不太对。

4,个人猜猜:由于最终转换成字符串的形式比较,所以感觉像是在做字符串比较而非时间比较导致数据不对。

5,问题:想问下大家有什么方法没用可以使出来的数据是正确的,说把列改类型是不可能了,只能从SQL语句上想办法了

PHP中文网PHP中文网2713 days ago453

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 16:33:29

    There is no problem in comparing datetime like this. Even if it is converted into characters, the comparison effect is the same. Why is your data wrong? You need to find other reasons

    reply
    0
  • 迷茫

    迷茫2017-04-17 16:33:29

    The solution is to use unix_timestamp to convert it. In the end, it will be no problem to use integer form for comparison.

    Excuse me, why can’t string comparison be used directly?

    reply
    0
  • Cancelreply