How to get single last record with a particular id in codeigniter

前端 未结 2 1541
灰色年华
灰色年华 2021-01-23 03:02

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 <

相关标签:
2条回答
  • 2021-01-23 03:14

    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
    
    0 讨论(0)
  • 2021-01-23 03:17

    Change this :
    $dataa = $query->result();
    to this :
    $dataa = $query->row();

    0 讨论(0)
提交回复
热议问题