select user_id, user_name,created_at, count(id) from message group by user_id order by created_at desc;
我想像这样编写 sql(mysql),如何使用 slick(不是普通的 sql)来实现。
最佳答案
Slick 尽可能模仿 Scala 的集合 API。想一想如何在 Scala 集合中做到这一点。
Messages.groupBy(_.userId).map{ case (userId,group) =>
(userId, group.userName.max, group.createdAt.max, group.size)
}.orderBy(_.createdAt)
(目前没有 .head
从集合中获取单个元素。使用 .max
作为后备。)
要获取 Messages 对象,请参阅文档,例如http://slick.typesafe.com/doc/2.0.0/code-generation.html
关于scala - Slick group by 与其他领域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21260282/