Select, count and where using codeigniter and mysql

前端 未结 4 1151
被撕碎了的回忆
被撕碎了的回忆 2021-01-12 07:31

I have two table as follow:

- tblSaler

    SalerID  |  SalerName | 
    ----------------------|
    1        |  sothorn   |
    ----------------------|
            


        
相关标签:
4条回答
  • 2021-01-12 08:15

    You can use this query for this

    SELECT
      tblSaler.SalerName,
      count(tblProduct.ProductID) as Total
    FROM tblSaler
      LEFT JOIN tblProduct
        ON tblProduct.SalerID = tblSaler.SalerID
    GROUP BY tblSaler.SalerID
    

    And here is the active record for this

    $select =   array(
                    'tblSaler.SalerName',
                    'count(tblProduct.ProductID) as Total'
                );  
    $this->db
            ->select($select)
            ->from('tblSaler')
            ->join('tblProduct','Product.SalerID = tblSaler.SalerID','left')
            ->group_by('tblSaler.SalerID')
            ->get()
            ->result_array();
    

    Demo

    OUTPUT

    _____________________
    | SALERNAME | TOTAL |
    |-----------|-------|
    |   sothorn |     1 |
    |      Daly |     2 |
    |    Lyhong |     3 |
    |   Chantra |     1 |
    _____________________           
    
    0 讨论(0)
  • 2021-01-12 08:23
       ## try this ##
       $this->db->select($column_name);
        $this->db->where($column_name,$type);
        $q=$this->db->get($table_name);
        $count=$q->result();
        return count($count);'
    
    0 讨论(0)
  • 2021-01-12 08:24

    Please try this code. Its working fine for me and it will help you also.

    $this->db->select('SalerName, count(*)');
    $this->db->from('tblSaler');        
    $this->db->join('tblProduct', 'tblSaler.SalerID = tblProduct.SalerID'); 
    $this->db->group_by('tblSaler.SalerID');       
    $query = $this->db->get();
    

    You can get whole SQL query using this line below

    $query = $this->db->get(); 
    echo $this->db->last_query();
    
    0 讨论(0)
  • 2021-01-12 08:29

    Try This

    $this->db->select('SalerName, count(*)'); 
    $this->db->from('tblSaler'); 
    $this->db->join('tblProduct', 'tblSaler.SalerID = tblProduct.SalerID');
    $this->db->group('SalerID');
    
    0 讨论(0)
提交回复
热议问题