简体   繁体   English

从mysql php中选择最后两行

[英]Select last two rows from mysql php

I have a database like this: 我有这样一个数据库:

+----------+----------+------+
| username | password | time |
+----------+----------+------+
| a        | b        | 1234 |
| c        | d        | 5678 |
| e        | f        | 9012 |
+----------+----------+------+

Now I have to arrange this by time so I ran: 现在我必须按时安排,所以我跑了:

mysql_query("SELECT * FROM `table` ORDER BY `time`")

This showed me rows arranged by time in ascending order, but I have to get only username c and e or I have to get the last two rows from the query. 这显示了按时间按升序排列的行,但是我必须只获取用户名ce或者我必须从查询中获取最后两行。

I have tried: 我努力了:

mysql_query('SELECT * FROM `table` ORDER BY `time` LIMIT 2')

But this showed me usernames a and c but I have to get the last two, how to do that? 但这显示了我的用户名ac但我必须得到最后两个,怎么做?

SELECT * FROM table
ORDER BY time DESC
LIMIT 2
SELECT * FROM table ORDER BY time DESC LIMIT 2

添加DESC关键字将按降序对结果进行排序。

尝试这个:

SELECT * FROM table ORDER BY time DESC LIMIT 2

尝试

mysqli_query('SELECT * FROM table ORDER BY time DESC LIMIT 2')

Assuming you have an id field with AUTO_INCREMENT set. 假设您有一个设置了AUTO_INCREMENT的id字段。

To get the last two from the table . 从表中获取最后两个。

SELECT * FROM table ORDER BY id DESC LIMIT 2 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM