流行度公式语法错误

时间:2015-03-12 18:26:44

标签: mysql ruby-on-rails postgresql

我不能为我的生活似乎弄清楚这段代码中的语法错误。我从here on SOhere我的rails应用程序中提取了一个用于受欢迎程度的公式。它与Hacker News使用的公式相同,看起来非常简单。然而,我一直得到一个我似乎可以解决的错误。

@statuses = Status.select("statuses.*, (((statuses.cached_votes_total - 1) / POW(((EXTRACT(EPOCH FROM (now()-statuses.created_at)) / 3600)::integer + 2), 1.5))) AS popularity").order("popularity DESC")

错误讯息:

  Status Load (73.1ms)  SELECT statuses.*, (((statuses.cached_votes_total - 1) / POW(((EXTRACT(EPOCH FROM (now()-statuses.created_at)) / 3600)::integer + 2), 1.5))) AS popularity FROM `statuses` ORDER BY popularity DESC
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EPOCH FROM (now()-statuses.created_at)) / 3600)::integer + 2), 1.5))) AS popular' at line 1: SELECT statuses.*, (((statuses.cached_votes_total - 1) / POW(((EXTRACT(EPOCH FROM (now()-statuses.created_at)) / 3600)::integer + 2), 1.5))) AS popularity FROM `statuses`   ORDER BY popularity DESC
  Rendered statuses/index.html.erb within layouts/application (95.7ms)
Completed 500 Internal Server Error in 299ms

ActionView::Template::Error (Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EPOCH FROM (now()-statuses.created_at)) / 3600)::integer + 2), 1.5))) AS popular' at line 1: SELECT statuses.*, (((statuses.cached_votes_total - 1) / POW(((EXTRACT(EPOCH FROM (now()-statuses.created_at)) / 3600)::integer + 2), 1.5))) AS popularity FROM `statuses`   ORDER BY popularity DESC):

我希望在MySQL和PG中比我更精通的人可以帮助我!提前谢谢!

1 个答案:

答案 0 :(得分:1)

对MySQL和PostgreSQL都不能有相同的查询。 MySQL EXTRACT(EPOCH FROM (...))的等价物是UNIX_TIMESTAMP(...)。你将不得不使用两个查询或编写包装函数。