【发布时间】:2019-07-10 01:27:07
【问题描述】:
想要所有不同用户的所有最新访问。
为此,我使用以下查询
Event.order(time: :desc).select('DISTINCT ON(user_id) user_id, time')
出现 SQL 语法错误。
ActiveRecord::StatementInvalid (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 'ON(user_id) user_id, time FROM `events` ORDER BY `events`.`time` DESC ' at line 1: SELECT DISTINCT ON(user_id) user_id, time FROM `events` ORDER BY `events`.`time` DESC LIMIT 11)
事件表列名
["id", "visit_id", "user_id", "name", "properties", "time"]
谁能帮帮我
预期的输出是这样的
{ 21 => "2018-12-18 09:44:59", 42 => "2018-12-19 12:08:59"}
21 是 user_id,value 是上次访问时间
使用mysql作为数据库
【问题讨论】:
-
ON(user_id)不是有效的 MySQL。 -
DISTINCT ON(columns). *是 PostgreSQL 语法,但您可以在 MySQL 中使用 SUBSTRING_INDEX(GROUP_CONCAT(..)) 语法解决它
标签: mysql ruby-on-rails activerecord