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

Php CodeIgniter:如何执行Select(不同的字段名)MySQL查询,php,mysql,codeigniter,distinct,Php,Mysql,Codeigniter,Distinct,我试图检索字段中所有唯一值的计数 SQL示例: SELECT count(distinct accessid) FROM (`accesslog`) WHERE record = '123' 如何在CodeIgniter内部执行此类查询 我知道我可以使用$this->db->query(),并编写自己的SQL查询,但我还有其他需要使用$this->db->where()。如果我使用->query(),尽管我必须自己编写整个查询 $record = '123'; $this->db-&g

我试图检索字段中所有唯一值的计数

SQL示例:

SELECT count(distinct accessid) FROM (`accesslog`) WHERE record = '123'
如何在CodeIgniter内部执行此类查询

我知道我可以使用
$this->db->query()
,并编写自己的SQL查询,但我还有其他需要使用
$this->db->where()
。如果我使用
->query()
,尽管我必须自己编写整个查询

$record = '123';

$this->db->distinct();

$this->db->select('accessid');

$this->db->where('record', $record); 

$query = $this->db->get('accesslog');
然后


应该有很长的路要走。

用下面的代码试试看

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

您还可以运行
->select('DISTINCT'field',FALSE)
,第二个参数告诉
CI
不要转义第一个参数


如果第二个参数为
false
,则输出将是
选择不同的“字段”
,而不是没有第二个参数,
选择不同的“字段”

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

$this->db->distinct();
$this->db->select('accessid');
$this->db->where('record', $record); 
$query = $this->db->get()->result_array();
return count($query);
$query = $this->db->distinct()->select('order_id')->get_where('tbl_order_details', array('seller_id' => $seller_id));
        return $query;
计算返回值的方法简单但有用:

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