Looking for laravel equivalent of a Query.
SELECT *
FROM (
SELECT * FROM tbl
ORDER BY updated_at
LIMIT 20
)
ORDER BY random()
LIMIT 5;
Please assume we have a model class "TableA" that already exists.
Inner query can be achieved using.
TableA::OrderByRaw('updated_at ASC NULLS FIRST')->limit(20)->get();
2
Answers
This is kind of hilarious, but I modified the query to below (for equivalent result).
This version ends up taking much less db memory (for processing) and has very similar execution time.
Please note : This works in my case, as My "in" clause will never exceed 150 elements.
This can be achieved in laravel using,
You can use a subquery to do this. I would prefer this way as it seems more readable.
Note For MySQL users, you can use joins to avoid
This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
error. Seehere
.