【问题标题】:SQLite3 output in reverse order latest 10 recordsSQLite3倒序输出最新10条记录
【发布时间】:2023-01-22 18:58:46
【问题描述】:

我需要最后 10 条记录。问题在于它的输出方式,因为从最新到最旧开始,我需要从最旧到新。见下文

sqlite> SELECT * FROM raw_all_sensors order by timestamp desc LIMIT 10;
2022-03-10 16:43:58|26.43|19.19|1014|56.81|0|0|813|0
2022-03-10 16:38:57|26.49|19.19|1014|55.22|0|0|813|0
2022-03-10 16:33:56|26.58|19.19|1014|55.67|0|0|813|0
2022-03-10 16:28:55|26.68|19.19|1014|56.08|0|0|813|0
2022-03-10 16:23:54|26.77|19.19|1014|55.72|0|0|813|0
2022-03-10 16:18:52|26.88|19.19|1014|56.49|0|0|813|0
2022-03-10 16:13:51|26.99|19.19|1014|56.47|0|0|813|0
2022-03-10 16:08:50|27.07|19.19|1014|56.04|0|0|813|0
2022-03-10 16:03:49|27.14|19.19|1014|55.98|0|0|813|0
2022-03-10 15:58:48|27.3|19.19|1014|56.37|0|0|813|0

但是我需要

2022-03-10 15:58:48|27.3|19.19|1014|56.37|0|0|813|0
2022-03-10 16:03:49|27.14|19.19|1014|55.98|0|0|813|0
2022-03-10 16:08:50|27.07|19.19|1014|56.04|0|0|813|0
...

我怎样才能做到这一点?

如果我做asc(而不是desc),它从最旧的记录开始,这不是我想要的。

【问题讨论】:

    标签: sqlite sql-order-by


    【解决方案1】:

    我相信你想要的是:

    select * from
    (SELECT * FROM raw_all_sensors order by timestamp desc LIMIT 10) t
    order by timestamp;
    

    基本上在派生表中获取您想要的 10 行,然后在外部查询中以升序方式对它们进行排序。

    【讨论】:

    • 你说对了 !!!!。谢谢你 。问题虽然。我在树莓派上运行这个,所以想知道这个解决方案是否会影响处理。任何想法?
    • 即使它在 sqlite 终端中工作,它在 python 脚本中也失败了。在 data_horaria cursor.execute("select * from (select (julianday(timestamp)-2440587.5)*86400000, round(temp1,2 ) as temp1, round(presion,2) as presion, round(humedad,2) as humedad, rain,aux1,temp2,aux2 from hourly_all_sensors order by timestamp desc limit 1000) order by timestamp") OperationalError: no such column: 时间戳-------------- 看起来超出范围
    • @Martin 对不起,我对 python 不是很熟悉。也许用有问题的 python 代码和你得到的错误发布另一个问题,我相信熟悉 python 的人能够提供帮助!
    • 最终在 python 中进行了反向操作。结果.reverse()
    猜你喜欢
    • 2015-11-07
    • 2012-12-10
    • 1970-01-01
    • 2019-12-28
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多