简体   繁体   English

如何使用时间戳从mysql中选择数据

[英]how to select data from mysql using timestamp

This query returns all the rows from my table, even where the end_timestamp is not defined: 此查询返回表中的所有行,即使未定义end_timestamp:

SELECT * FROM table 
    WHERE UNIX_TIMESTAMP(end_time) >= UNIX_TIMESTAMP('1448914804') 
    AND UNIX_TIMESTAMP(end_time) <= UNIX_TIMESTAMP('1448914804')

This query works fine but I want to pass timestamp: 此查询工作正常,但我想传递时间戳:

SELECT * FROM commercial 
    WHERE UNIX_TIMESTAMP(end_time) >= FROM_UNIXTIME('2015-11-30 20:14:00') 
    AND UNIX_TIMESTAMP(end_time) <= FROM_UNIXTIME('2015-11-30 20:14:05')

You are mixing up two types here. 您在这里混合了两种类型。

If you created end_time as an INTEGER / LONG , you could do this: 如果您将end_time创建为INTEGER / LONG ,则可以执行以下操作:

SELECT * FROM table 
    WHERE end_time >= 1448914804  
    AND   end_time <= 1448914804

SELECT * FROM table 
    WHERE end_time >= UNIX_TIMESTAMP('2015-11-30 20:14:00')
    AND   end_time <= UNIX_TIMESTAMP('2015-11-30 20:14:05')

or if it is a DATETIME or TIMESTAMP 还是DATETIMETIMESTAMP

SELECT * FROM table 
    WHERE end_time >= FROM_UNIXTIME( 1448914804 ) 
    AND   end_time <= FROM_UNIXTIME( 1448914804 )

SELECT * FROM table 
    WHERE end_time >= '2015-11-30 20:14:00' 
    AND   end_time <= '2015-11-30 20:14:05'

By the way, your first statement refers to table table and the second to commercial . 顺便说一句,您的第一个语句引用table table ,第二个语句引用commercial

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

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