【问题标题】:Scala Slick 3: Count Rows and get rowScala Slick 3:计数行并获取行
【发布时间】:2015-08-06 10:52:07
【问题描述】:

我不知道如何用 Slick (3) 计数。

val posts = for {
  p <- Posts.query if p.receiver === userId
  comments <- Comments.query if comments.postId === p.id
  author <- Users.query if p.author === author.id
  receiver <- Users.query if p.receiver === receiver.id
} yield (p, comments, author, receiver)

具有以下关系

Posts : Author : Receiver : Comments
1 : 1 : 1 : N

结果应该是:

Future[Seq[(Post, User, User, Int)]]

int 是 cmets grouped by Posts 的计数

有什么建议吗?

【问题讨论】:

    标签: sql scala slick slick-3.0


    【解决方案1】:

    您需要按帖子、作者和收件人以及地图对结果进行分组,以通过计数来聚合 cmets。

    val posts = (for {
      p <- Posts.query if p.receiver === userId
      comment <- Comments.query if comments.postId === p.id
      author <- Users.query if p.author === author.id
      receiver <- Users.query if p.receiver === receiver.id
    } yield (p, comment, author, receiver)) //So far thats your current query
      .groupBy({ //Group by post, author and receiver
          case (post, comment, author, receiver) =>
              (post, author, receiver)
      })
      .map({ //Aggregate your comments (second argument in tuple) and count them
          case ((post, author, receiver), list) => { 
              (post, author, receiver, list.map(_._2).count))
          }
      })
    

    目前在移动设备上,所以这可能无法编译,但你应该明白了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-22
      • 2017-03-15
      • 2012-12-10
      • 1970-01-01
      • 2017-03-07
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多