如何通过嵌套查询 select 来自 Slick 中 DateTime 的 table 的最新记录

How to select the most recent records from a table by DateTime in Slick, by nested query

如何根据日期时间字段获取最后 (x) 条记录。 通过使用 Slick、Scala 和 JodaTime 库? 因为下面的代码想要实现 last(x) 定义来解释问题。

import org.joda.time._
import com.github.tototoshi.slick.PostgresJodaSupport._

 def last(x: Int): DateTime=???

 val callInfo = callTable.filter( _.calltime >= last(x))

最后一个方法是否可以实现类似下面的东西?旨在生成嵌套查询

def last(x: Int) = {
    callTable.sortBy(_.calltime.desc).take(x).sortBy ( _.calltime.asc).take(1).map{ _.calltime}
  }

这个 return 一个查询[Rep[Option[DateTime]], Option[DateTime], Seq] 而不是 DateTime!!

我相信你想要的是对调用时间进行排序,然后取所需数量的元素:

callTable.sortBy(_.calltime.desc).take(x)

完整代码如下:

def last(x): DateTime = callTable.sortBy(_.calltime.desc).take(x).drop(x-1).map(_.calltime).headOption

val callInfo = last(10).map { lastDate => callTable.filter(_.calltime > lastDate).list