查询在一列中按日期和时间过滤,在另一列中按毫秒查询

时间:2014-04-15 07:40:34

标签: mysql

我有下表:

Time_Stamp ,Time_Stamp_ms
'2014-04-11 13:33:24', '879', '5555', '4444', '3333', '2222', '1111', '123', '1', 'text'
'2014-04-11 13:33:24', '899', '5555', '4444', '3333', '2222', '1111', '123', '2', 'text'
'2014-04-11 13:33:24', '919', '5555', '4444', '3333', '2222', '1111', '123', '3', 'text'
'2014-04-11 13:33:24', '939', '5555', '4444', '3333', '2222', '1111', '123', '4', 'text'
'2014-04-11 13:33:24', '959', '5555', '4444', '3333', '2222', '1111', '123', '5', 'text'
'2014-04-11 13:33:24', '979', '5555', '4444', '3333', '2222', '1111', '123', '6', 'text'
'2014-04-11 13:33:24', '999', '5555', '4444', '3333', '2222', '1111', '123', '7', 'text'
'2014-04-11 13:33:25', '19', '5555', '4444', '3333', '2222', '1111', '123', '8', 'text'
'2014-04-11 13:33:25', '39', '5555', '4444', '3333', '2222', '1111', '123', '9', 'text'
'2014-04-11 13:33:25', '59', '5555', '4444', '3333', '2222', '1111', '123', '10', 'text'
'2014-04-11 13:33:25', '79', '5555', '4444', '3333', '2222', '1111', '123', '11', 'text'
'2014-04-11 13:33:25', '99', '5555', '4444', '3333', '2222', '1111', '123', '12', 'text'

正如您所看到的,由于MySQL Server 5.5,我在单独的列中有Time_Stamp(日期时间)和Time_Stamp_ms(整数)。我的主要密钥是Time_StampTime_Stamp_ms

我想过滤发生的一切 2014-04-11 13:33:24.879 2014-04-11 13:33:25.79之间
并告诉我所有数据。

我尝试了几种变体:

SELECT * FROM table WHERE (concat(Time_Stamp,'.',Time_Stamp_ms)
BETWEEN '2014-04-11 13:33:24.879' AND '2014-04-11 13:33:25.79');

另外

SELECT * 
FROM table
WHERE ( '2014-04-11 13:33:24' < Time_Stamp
OR ('2014-04-11 13:33:24' = Time_Stamp AND '879' <= Time_Stamp_ms))
AND ( '2014-04-11 13:33:25' < Time_Stamp
OR ('2014-04-11 13:33:25' = Time_Stamp AND Time_Stamp_ms < '79'))

最后但并非最不重要:

SELECT * 
FROM table
WHERE ( '2014-04-11 13:33:24' < Time_Stamp
OR ('2014-04-11 13:33:24' = Time_Stamp AND 879 <= Time_Stamp_ms))
AND ( '2014-04-11 13:33:25' < Time_Stamp
OR ('2014-04-11 13:33:25' = Time_Stamp AND Time_Stamp_ms < 79))

这最后一个给了我没有意义的结果(例如它显示了2014-04-11 13:33:27的数据)

有没有人有任何想法?

2 个答案:

答案 0 :(得分:1)

所以也许只是因为你错误地键入了运算符..

试试这个:

SELECT * 
FROM table
WHERE ( '2014-04-11 13:33:24' < Time_Stamp
OR ('2014-04-11 13:33:24' = Time_Stamp AND 879 <= Time_Stamp_ms))
AND ( '2014-04-11 13:33:25' > Time_Stamp //--> here
OR ('2014-04-11 13:33:25' = Time_Stamp AND Time_Stamp_ms < 79))

答案 1 :(得分:0)

如果Time_Stamp_ms存储毫秒的值,则

select 
  @ms:=Time_Stamp_ms as millis, 
  @ms*1000 as micros, 
  cast( @ms/1000 as decimal(8,6) ) seconds
from my_table
limit 2

可能会导致

+--------+--------+----------+
| millis | micros | seconds  |
+--------+--------+----------+
|    879 | 879000 | 0.879000 |
|     79 |  79000 | 0.079000 |
+--------+--------+----------+

但是你试图像ts + .879ts + .79那样结束 这是错的。


我有以下解决方案编译 (我没有检查过.SQLFiddle关闭了:(()

milli seconds值转换为microseconds,并使用time_stamp列来获取所需时间戳值之间的记录。

以下查询可以缓解您在比较日期和毫秒时的问题。

select * from table_name
 where `Time_Stamp` + interval ( `Time_Stamp_ms` * 1000 ) microsecond
       between '2014-04-11 13:33:24.879'  -- <--- .879
           and '2014-04-11 13:33:25.079'; -- <--- .079