代码之家  ›  专栏  ›  技术社区  ›  Ian

CodeIgniter:如何执行Select(不同的字段名)MySQL查询

  •  40
  • Ian  · 技术社区  · 17 年前

    SQL示例:

    SELECT count(distinct accessid) FROM (`accesslog`) WHERE record = '123'
    

    $this->db->query() ,并编写自己的SQL查询,但我还有其他要求要使用 $this->db->where() ->query()

    4 回复  |  直到 14 年前
        1
  •  116
  •   bart_88    13 年前
    $record = '123';
    
    $this->db->distinct();
    
    $this->db->select('accessid');
    
    $this->db->where('record', $record); 
    
    $query = $this->db->get('accesslog');
    

    $query->num_rows();
    

    应该有很长的路要走。

        2
  •  12
  •   VxJasonxV    15 年前

    function fun1()  
    {  
       $this->db->select('count(DISTINCT(accessid))');  
       $this->db->from('accesslog');  
       $this->db->where('record =','123');  
       $query=$this->db->get();  
       return $query->num_rows();  
    }
    
        3
  •  12
  •   sauhardnc Ijaz Ahmed Bhatti    6 年前

    你也可以跑步 ->select('DISTINCT `field`', FALSE) 第二个参数告诉我们 CI

    第二个参数为 false ,则输出为 SELECT DISTINCT `field` 而不是没有第二个参数, SELECT `DISTINCT` `field`

        4
  •  4
  •   joash    7 年前

    由于计数是预期的最终值,因此在查询过程中

    $this->db->distinct();
    $this->db->select('accessid');
    $this->db->where('record', $record); 
    $query = $this->db->get()->result_array();
    return count($query);
    

    计数将返回返回的值

        5
  •  4
  •   Sani Kamal    7 年前

    简单但有用的方法:

    $query = $this->db->distinct()->select('order_id')->get_where('tbl_order_details', array('seller_id' => $seller_id));
            return $query;
    
    推荐文章