dongnai3960 2015-08-08 00:34
浏览 343
已采纳

SQL按升序选择最后10行

here's my current code:

$messages = mysqli_query($link, "SELECT `user`, `message` FROM `chat` ORDER BY `id` DESC LIMIT 10");
while($row = mysqli_fetch_array($messages)) {
    echo "<strong>".$row['user'].":</strong> ".safe_out($row['message'])."<br />";  
}

This prints the last 10 messages from the chat table in descending order. What I'm trying to do is print the last 10 messages in ascending order.

Changing DESC to ASC just prints out the first 10 messages, but I'm trying to get the last 10 messages to print in ascending order.

Do I need to put the mysqli_query results into an array and use reverse or is there an easier way?

Thanks

  • 写回答

1条回答 默认 最新

  • dongyan1993 2015-08-08 00:44
    关注

    You can use a derived table to re-sort the last 10 messages in ascending order

    SELECT * FROM (
      SELECT `id`, `user`, `message` FROM `chat` ORDER BY `id` DESC LIMIT 10
    ) t1 ORDER BY t1.id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)