Mohammad Masoudian
Mohammad Masoudian

Reputation: 3501

Select last 3 rows from database order by id ASC

how can i select last 3 rows from users table and then order it by id ascending?

for example :

id    |    username    |    password

1     |    user1       |    pass1

2     |    user2       |    pass2

3     |    user3       |    pass3

4     |    user4       |    pass4

5     |    user5       |    pass5

i want to select user5, user4 and user3 then order it by id ASCENDING

Upvotes: 4

Views: 14197

Answers (1)

shahbaz
shahbaz

Reputation: 372

Use this...

SELECT * FROM (
    SELECT * FROM table ORDER BY id DESC LIMIT 3
) sub
ORDER BY id ASC

Upvotes: 3

Related Questions