Rails 3 带有时间戳的聚合函数

发布于 2024-10-08 21:25:41 字数 225 浏览 0 评论 0原文

我想总结表中两个时间列之间的差异。我正在使用以下代码,但它返回零,因为 end_time 未正确解析。两列都是时间戳。

table.sum("end_time - start_time")

我发现当我输入 table.sum("end_time") 时,总和是 2010 年。这很奇怪,因为表中有一行的 end_time 为“2010-12-18 23:42:30”。

I want to sum up the differences between two time columns in a table. I'm using the following code but it returns zero because end_time is not getting parsed correctly. Both columns are timestamps.

table.sum("end_time - start_time")

I found that when I typed in table.sum("end_time"), the sum was 2010. This is odd because there is one row in table with an end_time of "2010-12-18 23:42:30".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里泪两行 2024-10-15 21:25:41

你不能“减去”时间,除非它们是 UNIX 时间戳...

只需让 Rails 完成这项工作即可。编写如下查询:

t = Table.find(:all, :conditions => {...})
t.each do |a|
  puts a.end_time - a.start_time
end

You can't "subtract" times unless they are UNIX timestamps...

Just let rails do the work. Write a query like:

t = Table.find(:all, :conditions => {...})
t.each do |a|
  puts a.end_time - a.start_time
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文