Slick 3.0.0 聚合查询 [英] Slick 3.0.0 Aggregate Queries
本文介绍了Slick 3.0.0 聚合查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对两个表进行复杂的查询.它们如下所示:
I'm trying to do a complex querying on two tables. They look like below:
表 1:
id:
name:
table2Id:
version:
表 2:
id:
comments:
假设我有合适的 Slick 类来表示这些表,我试图从满足以下条件的表 1 和表 2 中获取所有元素:
Assuming that I have the appropriate Slick classes that represents these tables, I'm trying to get all the elements from Table1 and Table 2 that satisfies the following condition:
Table1.table2Id === Table2.id and max(Table1.version)
我尝试了以下方法:
val groupById = (for {
elem1 <- table1Elems
elem2 <- table2Elems if elem1.id === elem2.id
} yield (elem1, elem2)).groupBy(_._1.id)
我知道我必须映射 groupById 并查找最大版本,但我没有正确理解语法!有什么帮助吗?
I know that I have to map the groupById and look for the max version, but I'm not getting the syntax right! Any help?
我需要的是与以下 SQL 查询等效的 Slick:
What I need is effectively Slick equivalent of the following SQL query:
SELECT *
FROM t t1
WHERE t1.rev = (SELECT MAX(rev) FROM t t2 WHERE t2.id = t1.id)
推荐答案
模拟:
SELECT * from t t1 WHERE t1.rev = (SELECT max(rev) FROM t t2 WHERE t2.id = t1.id)
尝试以下任一方法:
val q1 = for{
t < table1 if t.rev === (
table2.filter(_.id === t.id).map(_.rev).max
)
} yield t
val q2 = table1.filter{t=> t.rev === (
table2.filter(_.id === t.id).map(_.rev).max
)}.map(identity)
根据评论进行
val q = (for{
t1 < table1; t2 <- table2 if t1.id === t2.id
} yield t1).orderBy(_.version.max).reverse.take(1)
这篇关于Slick 3.0.0 聚合查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文