我有两个表:user
和payment
。
user
表格3 records
,每个2 payment records
表格中都有payment
。我想要用户的最后一笔付款细节,但不是获得3行,而是只获得1行。
$this->db->select('user.*, max(payment.create_at) as last_payment_at');
$this->db->from('user');
$this->db->join('payment','payment.user_id=user.id', 'left');
$this->db->order_by("user.id", "desc");
return $this->db->get()->result_object();
答案 0 :(得分:0)
$this->db->select('user.*, max(payment.create_at) as last_payment_at'); // you do not have to use max(payment.create_at) as last_payment_at if you are using desc and limit 1
$this->db->from('user');
$this->db->join('payment','payment.user_id=user.id', 'left');
$this->db->order_by("user.id", "desc"); // you can omit this line if you are getting max value
$this->db->limit(1); // <======= add limit for 1
return $this->db->get()->result_object();