Scala Slick 3:计算行并获取行
Scala Slick 3: Count Rows and get row
我不知道如何使用 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
是评论数 grouped by Posts
有什么建议吗?
您需要按 post、作者和接收者以及地图对您的结果进行分组,以通过计算评论来汇总评论。
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))
}
})
目前在移动设备上,所以这可能无法编译,但您应该明白了。
我不知道如何使用 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
是评论数 grouped by Posts
有什么建议吗?
您需要按 post、作者和接收者以及地图对您的结果进行分组,以通过计算评论来汇总评论。
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))
}
})
目前在移动设备上,所以这可能无法编译,但您应该明白了。