Welcome Guest, Not a member yet? Register   Sign In
return all row except last in
#1

[eluser]matches[/eluser]
Hello all,

What is the best way to return all rows except the most recent entry.

Does CI's database class of a good way to do this.

Thanks
#2

[eluser]osci[/eluser]
I don't think such a function would be needed in the db class.
You can always get the insert id and run a query excluding that id.
#3

[eluser]bproctor[/eluser]
There's probably a more efficient way to do it, but this ought to get the job done...

Code:
function get_all_but_last() {
   $q = $this->db->query('SELECT `id` FROM `my_table` ORDER BY `date_modified` DESC LIMIT 1');
   $exclude_id = $q->row()->id;
   $q = $this->db->query('SELECT * FROM `my_table` WHERE `id` != "'.$exclude_id.'"');
   return $q->result();
}
#4

[eluser]Aken[/eluser]
You can do so in your SQL query. Check out this Stack Overflow question.
#5

[eluser]Alfonso[/eluser]
Another way to do it , just removing the last element of the array $records

$query = $this->db->get('name_of_my_table');
$records = $query->result();
unset( $records[count($records)-1] );
#6

[eluser]jmadsen[/eluser]
Code:
SELECT *
FROM `my_table`
WHERE created_at != (SELECT MAX(mt2.created_at) FROM `my_table` mt2)

on large tables, this may not be the fastest way




Theme © iAndrew 2016 - Forum software by © MyBB