MYSQL从表中选择,获取表中的最新/最后10行

时间:2022-09-21 16:14:01

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

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

这显示前10行,而不是最后10行。

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

编辑:我想要最后10行(是的,DESC这样做)但是我希望它们以ASCENDING顺序返回。

3 个解决方案

#1


22  

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

要颠倒顺序(因此得到最后10而不是前10),使用DESC而不是ASC

EDIT

编辑

Based on your comment:

根据您的评论:

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

#2


1  

If you want the last 10 then just change ASC to DESC

如果你想要最后10个,那么只需将ASC更改为DESC

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

#3


0  

                $con = mysqli_connect("localhost","my_user","my_password","my_db");
                $limit = 10;                
                $query = "SELECT * FROM  $table";
                $resource = mysqli_query($con,$query);
                $total_rows = mysqli_num_rows($resource);
                $start = $total_rows-$limit;
                $query_limit= $query." LIMIT $start,$limit";

First I have set the limit

首先,我设定了限制

$limit = 10;

then

然后

 $total_rows = mysqli_num_rows($resource);

Here I have taken total number of rows affected.

在这里,我采取了受影响的总行数。

$start = $total_rows-$limit;

then substracted limit from number of rows to take starting record number

然后从行数减去限制以获取起始记录号

   $query_limit= $query." LIMIT $start,$limit";

and then added limit to the query. For more information about limit see this link https://www.w3schools.com/php/php_mysql_select_limit.asp

然后为查询添加限制。有关限制的更多信息,请参阅此链接https://www.w3schools.com/php/php_mysql_select_limit.asp

#1


22  

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

要颠倒顺序(因此得到最后10而不是前10),使用DESC而不是ASC

EDIT

编辑

Based on your comment:

根据您的评论:

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

#2


1  

If you want the last 10 then just change ASC to DESC

如果你想要最后10个,那么只需将ASC更改为DESC

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

#3


0  

                $con = mysqli_connect("localhost","my_user","my_password","my_db");
                $limit = 10;                
                $query = "SELECT * FROM  $table";
                $resource = mysqli_query($con,$query);
                $total_rows = mysqli_num_rows($resource);
                $start = $total_rows-$limit;
                $query_limit= $query." LIMIT $start,$limit";

First I have set the limit

首先,我设定了限制

$limit = 10;

then

然后

 $total_rows = mysqli_num_rows($resource);

Here I have taken total number of rows affected.

在这里,我采取了受影响的总行数。

$start = $total_rows-$limit;

then substracted limit from number of rows to take starting record number

然后从行数减去限制以获取起始记录号

   $query_limit= $query." LIMIT $start,$limit";

and then added limit to the query. For more information about limit see this link https://www.w3schools.com/php/php_mysql_select_limit.asp

然后为查询添加限制。有关限制的更多信息,请参阅此链接https://www.w3schools.com/php/php_mysql_select_limit.asp