【问题标题】:Select, count and where using codeigniter and mysql使用 codeigniter 和 mysql 选择、计数和位置
【发布时间】:2013-11-06 19:02:10
【问题描述】:

我有两个表如下:

- tblSaler

    SalerID  |  SalerName | 
    ----------------------|
    1        |  sothorn   |
    ----------------------|
    2        |  Daly      |  
    ----------------------|
    3        |  Lyhong    |
    ----------------------|
    4        | Chantra    |
    ----------------------|

- tblProduct

ProductID  | Product  | SalerID |
--------------------------------|
1          | Pen      | 3       |
--------------------------------|
2          | Book     | 2       |
--------------------------------|
3          | Phone    | 3       |
--------------------------------|
4          | Computer | 1       |
--------------------------------|
5          | Bag      | 3       |
--------------------------------|
6          | Watch    | 2       |
--------------------------------|
7          | Glasses  | 4       |
--------------------------------|

我需要的结果是:

sothorn | 1
Daly    | 2
Lyhong  | 3
Chantra | 1

我试过了:

    $this->db->select('count(SalerName) as sothorn where tblSaler.SalerID = 1, count(SalerName) as Daly where tblSaler.SalerID = 2, count(SalerName) as Lyhong where tblSaler.SalerID = 3, count(SalerName) as Chantra where tblSaler.SalerID = 4');
    $this->db->from('tblSaler');
    $this->db->join('tblProduct', 'tblSaler.SalerID = tblProduct.SalerID');

【问题讨论】:

    标签: mysql codeigniter


    【解决方案1】:

    您可以为此查询使用此查询

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

    这里是这个的活动记录

    $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

    输出

    _____________________
    | SALERNAME | TOTAL |
    |-----------|-------|
    |   sothorn |     1 |
    |      Daly |     2 |
    |    Lyhong |     3 |
    |   Chantra |     1 |
    _____________________           
    

    【讨论】:

      【解决方案2】:

      请尝试此代码。它对我来说很好,它也会帮助你。

      $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();
      

      您可以使用下面的这一行获取整个 SQL 查询

      $query = $this->db->get(); 
      echo $this->db->last_query();
      

      【讨论】:

        【解决方案3】:

        试试这个

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

        【讨论】:

          【解决方案4】:
             ## 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);'
          

          【讨论】:

            猜你喜欢
            • 2012-04-04
            • 2015-06-06
            • 1970-01-01
            • 2015-07-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多