简体   繁体   中英

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.

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?

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.

To get the last two from the table .

SELECT * FROM table ORDER BY id DESC LIMIT 2 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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