0

我的数据库结构如下所示:

编号 | 内容

我要获取最大 id 的条目(不仅仅是 id)。

我阅读了答案How to make aggregations with slickfirst ,但我发现语句中没有方法: Query(Coffees.map(_.price).max).first。现在该怎么做?

如果我需要contentmax 的项目id怎么办?

4

1 回答 1

0

要检索另一列,您可以执行以下操作。下面的示例计算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]]
于 2017-08-21T15:47:10.800 回答