Amir Sadegh
Amir Sadegh

Reputation: 127

Select last 3 rows of sql table

I want to select the last 3 rows of an sql table. I know I should use SELECT * FROM table ORDER BY DESC LIMIT 3, but the problem with this code is that it selects the rows from the end. For example, it selects 30, then 29, then 28. But, I need them in this format: 28, 29, 30. Any suggestion?

Upvotes: 3

Views: 47868

Answers (7)

Devendra Baghel
Devendra Baghel

Reputation: 1

It will help you out to give latest 3 rows data, if you wanna take first 3 rows then ASC instead of DESC.

select distinct column_name from  Table order by column_name desc limit 3;

Upvotes: 0

Hasan ZainAlDeen
Hasan ZainAlDeen

Reputation: 1

try this manual one !

easy and Simple !!

Select * From tableName where 
PKCol=(select count(*) from tableName ) 
OR 
PKCol=(select count(*) from tableName )-1 
OR 
PKCol=(select count(*) from tableName )-2 
order by PKCol desc;

Upvotes: 0

null.point3r
null.point3r

Reputation: 1103

Try this:

SELECT * FROM (
  SELECT * FROM reset ORDER BY id DESC LIMIT 3
) as r ORDER BY id

Upvotes: 8

Yajur
Yajur

Reputation: 1

try

Select * from (SELECT * FROM Table_name ORDER BY Column_name DESC limit 0,3) as alias ORDER BY Column_name ASC;

Upvotes: 0

errorintheapplication
errorintheapplication

Reputation: 155

I hope this help your problem

select * from
(
select *  from reset 
order by id DESC LIMIT 3
 ) t
order by id ASC 

Upvotes: 3

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

Try something like this:-

  SELECT * FROM reset 
  WHERE username = '$table' ORDER BY id  ASC LIMIT (FOUND_ROWS() - 3), 3

Upvotes: 1

Colin 't Hart
Colin 't Hart

Reputation: 7729

How about something like:

select * from (select * from table order by x desc limit 3) order by x;

Upvotes: 0

Related Questions