Sql Select Last N Rows, Sort Them Reversed

sql select last n rows, sort them reversed

Use a subquery:

SELECT ce.*
FROM (SELECT fecha_hora, estado_viejo
FROM cambios_de_estado
WHERE usuario_id= '35512'
ORDER BY fecha_hora DESC
LIMIT 9
) ce
ORDER BY fecha_hora ASC

Select Top N Records Ordered by X, But Have Results in Reverse Order

SELECT * FROM 
(SELECT TOP 10 * FROM FooTable ORDER BY X DESC) as myAlias
ORDER BY X ASC

i.e. you might need an alias on your subquery, but other than that it should work.

SQL select last 10 rows in ascending order

You can use a derived table to re-sort the last 10 messages in ascending order

SELECT * FROM (
SELECT `id`, `user`, `message` FROM `chat` ORDER BY `id` DESC LIMIT 10
) t1 ORDER BY t1.id

Laravel : How to take last n(any number) rows after ordered in ascending order?

You are very close.

It sounds like you want to first order the array by descending order

  Model::orderBy('created_at','desc')->take(3);

but then reverse the array. You can do this one of two ways, either the traditional PHP (using array_reverse).

  $_dates = Model::orderBy('created_at','desc')->take(3);
$dates = array_reverse($_dates);

Or the laravel way, using the reverse function in Laravel's Collection class.

  $_dates = Model::orderBy('created_at','desc')->take(3)->reverse();

Check out Laravel's Collection documentation at their API site at http://laravel.com/api/class-Illuminate.Support.Collection.html

Now $dates will contain the output you desire.

dunno,time3
world,time4
hihio,time5

Select last N rows from MySQL

You can do it with a sub-query:

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

This will select the last 50 rows from table, and then order them in ascending order.



Related Topics



Leave a reply



Submit