Welcome Guest, Not a member yet? Register   Sign In
Get last two records from table using active records
#1

[eluser]ibnclaudius[/eluser]
How can I get the last two records from a table?

For example I have this:

id | update
1 | a
2 | b
3| c
4 | d
5 | e

I want to return it like this:

4 | d
5 | e

with order_by(id, 'DESC'), it returns:

5 | e
4 | d

How can I do this with active records?
#2

[eluser]CroNiX[/eluser]
I think in that case it would be much easier and simpler to just run array_reverse() on the returned result.
#3

[eluser]ibnclaudius[/eluser]
Thanks! Worked!

#4

[eluser]rip_pit[/eluser]
in mysql you can use something like :

Code:
(SELECT t1.* FROM (SELECT t2.* FROM table2 AS t2 ORDER BY t2.id DESC LIMIT 2) AS t1 ORDER BY t1.id ASC)

Using AR, this should works :

Code:
($this->db->query("(SELECT t1.* FROM (SELECT t2.* FROM table2 AS t2 ORDER BY t2.id DESC LIMIT 2) AS t1 ORDER BY t1.id ASC)");

#5

[eluser]CroNiX[/eluser]
Which will take more memory and resources than just reversing the simple array. Run an explain on that to see. Also, that isn't an active record query, that's a straight up query.
#6

[eluser]rip_pit[/eluser]
i know but it's a pure mysql oriented query without need of php that can sometimes helpfull
ibnclaudius can choose the one that feet best his needs Wink




Theme © iAndrew 2016 - Forum software by © MyBB