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
Adding the DESC
keyword will sort the results in descending order.
Try this:
SELECT * FROM table ORDER BY time DESC LIMIT 2
Try
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