想要所有不同用户的所有最新访问。
为此,我使用下面的查询
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作为数据库
因此您希望所有用户访问都包含上次访问时间。
而不是使用DISTINCT
功能,你可以使用GROUP
with MAX
功能。
查询看起来像
Events.group(:user_id).maximum(:time)
这输出你想要的结果
{21=>Tue, 18 Dec 2018 11:15:24 UTC +00:00, 23=>Thu, 20 Dec 2018 06:42:10 UTC +00:00}
希望这对你有用。
供参考
区别于(列)。是 PostgreSQL 语法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)