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

时间:2018-10-09 10:10:23

标签: php cakephp count distinct

我正在尝试在具有where条件的表上实现不重复计数。

这是我尝试过的:

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

但是它不起作用。结果为25,但应该为2Distinct无法正常工作。

1 个答案:

答案 0 :(得分:1)

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