我的数据库结构如下所示:
编号 | 内容
我要获取最大 id 的条目(不仅仅是 id)。
我阅读了答案How to make aggregations with slickfirst
,但我发现语句中没有方法: Query(Coffees.map(_.price).max).first
。现在该怎么做?
如果我需要content
max 的项目id
怎么办?
我的数据库结构如下所示:
编号 | 内容
我要获取最大 id 的条目(不仅仅是 id)。
我阅读了答案How to make aggregations with slickfirst
,但我发现语句中没有方法: Query(Coffees.map(_.price).max).first
。现在该怎么做?
如果我需要content
max 的项目id
怎么办?
要检索另一列,您可以执行以下操作。下面的示例计算max
一列的值,找到具有该最大值的行,并返回该行中另一列的值:
val coffees = TableQuery[Coffees]
val mostExpensiveCoffeeQuery =
for {
maxPrice <- coffees.map(_.price).max.result
c <- maxPrice match {
case Some(p) => coffees.filter(_.price === p).result
case None => DBIO.successful(Seq())
}
} yield c.headOption.map(_.name)
val mostExpensiveCoffee = db.run(mostExpensiveCoffeeQuery)
// Future[Option[String]]
或者,要返回一个完整的Coffees
对象:
val mostExpensiveCoffeeQuery =
for {
...
} yield c.headOption
val mostExpensiveCoffee = db.run(mostExpensiveCoffeeQuery)
// Future[Option[Coffees]]