Here i have 2 tables. First one is customer and second on is membership
Now i just want to get a single last record with a particular <
To get last record for each customer from membership table based on highest id you can do a self join to membership by tweaking the joining part like
$this->db->select('c.*,m.*');
$this->db->from('customer as c');
$this->db->join('membership as m', 'c.id = m.customer_id', 'left');
$this->db->join('membership as m1', 'm.customer_id = m1.customer_id AND m.id < m1.id', 'left');
$this->db->where('m1.id IS NULL', null, false)
$query = $this->db->get();
A plain SQL would be something like
SELECT c.*,m.*
FROM customer AS c
LEFT JOIN membership AS m ON c.id = m.customer_id
LEFT JOIN membership AS m1 ON m.customer_id = m1.customer_id
AND m.id < m1.id
WHERE m1.id IS NULL