繁体   English   中英

通过codeigniter中的查询计算具有详细信息的行数

[英]count number of rows with detail through query in codeigniter

我正在codeigniter建立一个旅行社网站。 我从我的数据库中获取非洲的目的地并在页面上显示。 这是我的控制器代码:

public function africa() {
    $data['africa']= $this->Travel->africa_des();
    $this->load->view('africa', $data);
} 

我的型号:

function africa_des() {
    $this->db->distinct();
    $this->db->select();
    $this->db->from('travels_detail');
    $this->db->like('region', 'africa');
    $this->db->limit(5);
    $query= $this->db->get();

    return $query->result_array();
}

我想获得我在数据库中拥有的每个目的地的总航班数。 例如,我有25个阿布贾航班,所以我想从阿布贾目的地获得这个号码。

任何人都有任何想法?

试试这个吧

public function africa_desc(){
    $this->db->distinct();
    $this->db->select('*');
    $this->db->from('travels_detail');
    $this->db->where('region', 'africa');
    $this->db->limit(5);
    $query= $this->db->get();
    return $query->result_array();
}

当你使用$this->db->distinct() ,必须使用$this->db->group_by('column_name')请参阅此答案https://stackoverflow.com/a/19509869/6098616 在您的情况下,我将假设destination是您的列名称。

 public function africa_desc(){ $this->db->select('*'); $this->db->distinct(); $this->db->group_by('destination'); $this->db->from('travels_detail'); $this->db->where('region', 'africa'); $this->db->limit(5); $query= $this->db->get(); return $query->result_array(); } 

希望这会有所帮助。

试试这个计算航班

public function africa_desc(){    
$this->db->select('*');
$this->db->distinct();
$this->db->group_by('destination');
$this->db->from('travels_detail');
$this->db->where('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->num_rows();

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM