Android SQLite 查询 - 获取最新的 10 条记录 [英] Android SQLite Query - Getting latest 10 records

查看:50
本文介绍了Android SQLite 查询 - 获取最新的 10 条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Android 应用程序中保存了一个数据库,我想检索插入到数据库中的最后 10 条消息.

I have a database saved in my Android application and want to retrieve the last 10 messages inserted into the DB.

当我使用时:

Select * from tblmessage DESC limit 10;

它给了我 10 条消息,但是来自 TOP.但我想要最后 10 条消息.可能吗?

it gives me the 10 messages but from the TOP. But I want the LAST 10 messages. Is it possible?

假设整个表数据为-

1,2,3,4,5....30

我写了查询 select * from tblmessage where timestamp desc limit 10

显示30,29,28...21

但我希望序列为 - 21,22,23...30

推荐答案

将 DESC 更改为 ASC,您将获得您想要的记录,但如果您需要将它们排序,那么您将需要颠倒它们来的顺序in.您可以在自己的代码中执行此操作,也可以像这样简单地扩展您的查询:

Change the DESC to ASC and you will get the records that you want, but if you need them ordered, then you will need to reverse the order that they come in. You can either do that in your own code or simply extend your query like so:

select * from (
    select *
    from tblmessage
    order by sortfield ASC
    limit 10
) order by sortfield DESC;

您确实应该始终指定 order by 子句,而不仅仅是 ASC 或 DESC.

You really should always specify an order by clause, not just ASC or DESC.

这篇关于Android SQLite 查询 - 获取最新的 10 条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆