【问题标题】:Get the last row in Slick获取 Slick 中的最后一行
【发布时间】:2021-08-25 00:38:08
【问题描述】:

我有 PostgreSQL 表。

消息:

 |id  |  phone | message | login |
-----------------------------------
* |1   | 85543422 | Hello!  | User1 |
  |2   | 85543422 | i love  | User2 |
-----------------------------------*

我需要按电话值 (85543422) 过滤表格并从最后一行获取用户名。 现在我有一个方法可以让你从第一行提取用户名。

//return "User1"
    def getUserByPhone():String = {  
          val query = outgoing.filter(_.phone === "85543422").map(_.login).result.headOption
          val result = Await.result(db.run(query), Duration.Inf)
          if (result.value.isEmpty) return "None"
          result.value.get
      }

帮助实现从最后一行删除用户名的方法。 我需要得到“User2”

【问题讨论】:

    标签: sql postgresql scala slick


    【解决方案1】:

    您可以按 id desc 排序以获取最后一行的用户名

    例如

     val query = outgoing.filter(_.phone === "85543422")
                         .sortBy(_.id.desc)
                         .map(_.login).result.headOption
    

    【讨论】:

    • 哇!这是个好主意!好的,我试试看。
    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多