简体   繁体   English

从MySQL中选择时间戳

[英]select timestamp from mysql

I have a problem with query where the conditions are related to TIMESTAMP column. 我对条件与TIMESTAMP列相关的查询有疑问 So I have a table named table1 which has two columns start_time and end_time . 因此,我有一个名为table1的表,该表具有两列start_timeend_time Both of them store TIMESTAMP . 他们两个都存储TIMESTAMP I want to query my databse for a particular rows where end_time is higher than given timestamp value and lower than given timestamp value. 我想查询我的数据库来end_time高于给定时间戳记值且低于给定时间戳记值的特定行。 Basically I want to get all the rows where end_time is between given TIMESTAMP range. 基本上,我想获取end_time在给定TIMESTAMP范围之间的所有行。

I have found that in my case following query works: 我发现以下查询有效:

SELECT * FROM table1
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')

But it is not what I want to. 但这不是我想要的。 I want to give TIMESTAMP value instead of DATE . 我想给TIMESTAMP值,而不是DATE

What is also strange is that this query returns values that have end_time of earlier date than given timestamp: 还奇怪的是,此查询返回的值的end_time早于给定时间戳记的值:

SELECT * FROM table1 WHERE end_time > FROM_UNIXTIME( '2015-11-30 20:20:05' )

Query above returns rows where end_timestamp is 2015-11-30 20:18:05 上面的查询返回end_timestamp2015-11-30 20:18:05的

I feel that I am missing something here. 我觉得我在这里错过了一些东西。 Should I use INT instead of TIMESTAMP for my end_time ? 我是否应该使用INT而不是TIMESTAMP作为我的end_time I believe that the following query should return expected values but it gives me nothing: 我相信以下查询应返回期望值,但它什么也没有给我:

SELECT * FROM table1
WHERE end_time >= FROM_UNIXTIME( 1448914740 ) 
AND end_time <= FROM_UNIXTIME( 1448914810 )

You don't need to convert your WHERE clause values using FROM_UNIXTIME . 您不需要使用FROM_UNIXTIME转换WHERE子句值。 FROM_UNIXTIME converts a TIMESTAMP into a date. FROM_UNIXTIME将TIMESTAMP转换为日期。 If you're sending values to the SQL statement that are already in TIMESTAMP format, your SQL will look like this: 如果您要向SQL语句发送已经为TIMESTAMP格式的值,则SQL将如下所示:

SELECT * FROM table1 WHERE end_time >= 1448914740 AND end_time <= 1448914810

If you are sending date strings (eg '2015-11-30 20:14:00' and '2015-11-30 20:14:05') then your SQL would look like this: 如果您要发送日期字符串(例如'2015-11-30 20:14:00'和'2015-11-30 20:14:05'),则您的SQL如下所示:

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

Since you're storing your values in your table as TIMESTAMP I would recommend you use the first SQL statement. 由于您将值以TIMESTAMP存储在表中,因此建议您使用第一个SQL语句。

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

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