本文介绍了MYSQL从表中选择,获取表中的最新/最后10行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好,最简单的方法?我的查询当前是:

What's the best, and easiest way to do this? My query currently is:

  SELECT *
    FROM chat
   WHERE (userID = $session AND toID = $friendID)
      OR (userID = $friendID AND toID = $session)
ORDER BY id
   LIMIT 10

尽管如此,它显示的是前10行,而不是最后10行.

This shows the first 10 rows though, not the last 10.

我想要最后10行(这是,DESC这样做),但是我希望它们以升序返回.

I Want the last 10 rows (Which yes, DESC does this) However I want them to be returned in ASCENDING order.

推荐答案

要颠倒顺序(因此获取最后10个而不是前10个),请使用DESC代替ASC

to reverse the order (therefore get last 10 instead of first 10), use DESC instead of ASC

编辑

根据您的评论:

SELECT * FROM (
  SELECT *
  FROM chat
  WHERE (userID = $session AND toID = $friendID)
    OR (userID = $friendID AND toID = $session)
  ORDER BY id DESC
  LIMIT 10
) AS `table` ORDER by id ASC

这篇关于MYSQL从表中选择,获取表中的最新/最后10行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:05