【问题标题】:How does sorting work with limit in kotlin exposed model?在 kotlin 暴露模型中,排序如何与限制一起工作?
【发布时间】:2019-03-19 07:35:25
【问题描述】:

我有以下 sn-p 代码:

UserDataModel
                .find {
                    UserDataTable.type eq type and (
                            UserDataTable.userId eq userId
                            )
                }
                .limit(count)
                .sortedByDescending { it.timestamp }

sortedByDescending 是 kotlin 集合 API 的一部分。我主要关心的是:如果选择查询看起来像这样并且不包含 ORDER BY 子句,那么暴露的 lib 如何从表中返回 top(根据时间戳)count 行?

SELECT USERDATA.ID, USERDATA.USER_ID, USERDATA.TYPE,
USERDATA.PAYLOAD, USERDATA."TIMESTAMP"
FROM USERDATA 
WHERE USERDATA.TYPE = 'testType'
and USERDATA.USER_ID = 'mockUser'
LIMIT 4

对于相同的数据,有时或以某种方式返回的结果是否可能不同?

在这里真的很挣扎。提前谢谢你。

【问题讨论】:

    标签: kotlin kotlin-exposed


    【解决方案1】:

    您在执行查询之后对结果进行排序。

    您需要使用docs中描述的orderBy方法

    UserDataModel
      .find {
        UserDataTable.type eq type and (UserDataTable.userId eq userId)
      }
      .limit(count)
      .orderBy(UserDataTable.timestamp to SortOrder.DESC)
    

    【讨论】:

    • .orderBy(UserDataTable.timestamp to false) 已弃用,取而代之的是 .orderBy(UserDataTable.timestamp to SortOrder.DESC)。我会更新维基页面。
    • 我的问题是暴露的过时版本(0.11)。它不包含 orderBy 方法。可能这些信息会对某人有所帮助
    猜你喜欢
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多