【问题标题】:Slick group by with other fieldsSlick group by 与其他字段
【发布时间】:2014-01-21 14:00:25
【问题描述】:
select user_id, user_name,created_at, count(id) from message group by user_id order by created_at desc;

我想像这样写 sql(mysql),如何使用 slick(不是普通的 sql)来实现。

【问题讨论】:

  • “快速介绍”应该get you started.
  • @fotNelton 我实际上已经读过了。但仍然不容易

标签: scala slick


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 2021-10-03
    • 2014-05-27
    • 2016-02-09
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多