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
Column Is of Type Timestamp Without Time Zone But Expression Is of Type Character
Check If Column Exists Before Executing Oracle
How to See Query History in SQL Server Management Studio
How to Strip All Non-Alphabetic Characters from String in SQL Server
How to Convert a Timestamp (Date Format) to Bigint in SQL
How to Insert an Image in Sqlite Database(Table)
Return Dummy Value If No Row Present for in Clause
Ssis Date in Derived Column as Yyyy-Mm-Dd Format
How to Subtract One Day from Current Date Then Convert to String in Hive
Spring Data JPA Utf-8 Encoding Not Working
Pg_Restore: [Archiver] Unsupported Version (1.14) in File Header
Ssis Failed Validation and Returned Validation Status "Vs_Isbroken"
Using SQL to Determine Word Count Stats of a Text Field
How to Use Json_Extract in MySQL and Get a String Without the Quotes
Selecting a Single Record from the Same Multiple Rows from a Single Table
How to Store Output of a SQL Server Stored Procedure in a .Txt File
How to Use SQL Like Condition With Multiple Values in Postgresql