简体   繁体   English

CakePHP 3.6:从表中计算不同的记录

[英]CakePHP 3.6: Count distinct records from table

I am trying to achieve a distinct count on a table with a where condition.我试图在具有 where 条件的表上实现不同的计数。

This is what I have tried:这是我尝试过的:

$customerServiceTypes = TableRegistry::get('CustomerServiceTypes');
$customers_count = $customerServiceTypes->find('all', array(
            'fields' => 'DISTINCT CustomerServiceType.customer_id',
            'conditions' => array("CustomerServiceTypes.service_type_id" => $id)))->count();

But its not working.但它不起作用。 I get 25 as result but it should be 2 .结果我得到25但它应该是2 Distinct is not working. Distinct不起作用。

$customerServiceTypes = TableRegistry::get('CustomerServiceTypes');
$customers_count = $customerServiceTypes->find()
     ->select(['customer_id'])
     ->distinct()
     ->where(['service_type_id =' => $id])->count();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM