Ian*_*Ian 48 php mysql codeigniter distinct
我正在尝试检索字段中所有唯一值的计数.
示例SQL:
SELECT count(distinct accessid) FROM (`accesslog`) WHERE record = '123'
Run Code Online (Sandbox Code Playgroud)
我怎么能在CodeIgniter里面做这种查询?
我知道我可以使用$this->db->query()
,并编写自己的SQL查询,但我有其他要求,我想用它$this->db->where()
.如果我使用->query()
虽然我必须自己编写整个查询.
Mar*_*win 99
$record = '123';
$this->db->distinct();
$this->db->select('accessid');
$this->db->where('record', $record);
$query = $this->db->get('accesslog');
Run Code Online (Sandbox Code Playgroud)
然后
$query->num_rows();
Run Code Online (Sandbox Code Playgroud)
应该走很远的路.
use*_*non 10
尝试使用以下代码
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();
}
Run Code Online (Sandbox Code Playgroud)
小智 10
你也可以运行- > select('DISTINCT`field`',FALSE),第二个参数告诉CI不要转义第一个参数.使用第二个参数,输出将是SELECT DISTINCT`field`而不是没有第二个参数,SELECT`DISTINCT``field`
由于计数是预期的最终值,因此在查询过程中
$this->db->distinct();
$this->db->select('accessid');
$this->db->where('record', $record);
$query = $this->db->get()->result_array();
return count($query);
Run Code Online (Sandbox Code Playgroud)
返回值的计数
简单但有用的方法:
$query = $this->db->distinct()->select('order_id')->get_where('tbl_order_details', array('seller_id' => $seller_id));
return $query;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
131003 次 |
最近记录: |