【问题标题】:last 20 data in ascending order in phpphp中按升序排列的最后20个数据
【发布时间】:2017-07-27 07:57:04
【问题描述】:

如何在php查询中获取数据库的最后20个数据,这些数据将以升序出现在数据库中?我问是因为如果我写 desc limit 20 那么它将首先显示最后一个数据,最后显示最后 20 个数据。现在如果我写 asc limit 20 那么它将给出表中的前 20 个数据。 我正在使用查询 select * from table_name order by id desc limit 20 最后 20 条数据。

【问题讨论】:

  • 使用 asc 代替 desc
  • @scaisEdge 这将返回 first 20 行。 OP 希望最后 20 行升序。
  • 我已经编辑了我的问题,我想要最后 20 个数据,但按升序排列。

标签: php html sql database


【解决方案1】:
SELECT * FROM
(
SELECT * FROM `table_name` ORDER BY `id` DESC LIMIT 20
)
AS temp
ORDER BY `id` ASC

选择最后 20 个,然后用 asc 排序

【讨论】:

    【解决方案2】:

    如果您需要所有 desc 中的 20 个 asc,您可以使用

    select * from (
         select * from table_name order by id desc 
     ) t order by id asc limit 20
    

    【讨论】:

    • 我认为你的结果与:select * from table_name order by id asc limit 20 :)
    • @sukalogika no this from the reslt indescent ordered invert the top 20
    • @scaisEdge,与 sukalogika 的答案相比,这个查询有什么优势(性能)吗?只是想知道..
    • @AbdulRasheed 好问题......有序选择中的限制子句应用于结果......所以没有明显的结果......因为所有的选择和排序都是在限制过滤器之前执行的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    相关资源
    最近更新 更多