Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select last two rows from mysql php

Tags:

sql

php

mysql

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?

like image 584
Mrigank Avatar asked Dec 02 '25 09:12

Mrigank


1 Answers

SELECT * FROM table
ORDER BY time DESC
LIMIT 2
like image 126
Nathan Avatar answered Dec 04 '25 23:12

Nathan