php codeigniter如何分组并按顺序显示最后一个值

时间:2017-09-09 06:32:15

标签: php mysql codeigniter

如何从codeigniter中的mysql获取每个v_id的最后一个值

public function allot_fuel_info_by_month($fuel_allot_month = NULL) {
            $this->db->select('tbl_allot_fuel.*', FALSE);
            $this->db->select('tbl_vehicle.*', FALSE);
            $this->db->select('tbl_school.*', FALSE);
            $this->db->select('tbl_vehicle_vendor.*', FALSE);
            $this->db->select('tbl_vehicle_route.*', FALSE);
            $this->db->from('tbl_allot_fuel');
            $this->db->join('tbl_vehicle', 'tbl_vehicle.v_id  = tbl_allot_fuel.v_id', 'left');
            $this->db->join('tbl_school', 'tbl_school.school_id  = tbl_allot_fuel.school_id', 'left');
            $this->db->join('tbl_vehicle_vendor', 'tbl_vehicle_vendor.v_vendor_id  = tbl_allot_fuel.v_vendor_id', 'left');
            $this->db->join('tbl_vehicle_route', 'tbl_vehicle_route.v_route_id  = tbl_allot_fuel.v_route_id', 'left');
            $this->db->where('tbl_allot_fuel.fuel_allot_month', $fuel_allot_month);
            $this->db->order_by('tbl_allot_fuel.v_id', 'DECS');
            $this->db->group_by('tbl_allot_fuel.v_id');
            $query_result = $this->db->get();
            $result = $query_result->result();
            return $result;
    }

如何从codeigniter中的mysql获取每个v_id的最后一个值。 表格式是

allot_id | v_id | fuel_allot_date | issue_fuel | pending_fuel 

1 个答案:

答案 0 :(得分:0)

您应该使用DESC而不是DECS并使用限制1来获取一行

 $this->db->limit(1);
 $this->db->order_by('tbl_allot_fuel.v_id', 'DESC');