首页 > 解决方案 > CakePHP 3.6:计算表中不同的记录

问题描述

我正在尝试在具有 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不管用。

标签: phpcakephpcountdistinct

解决方案


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

推荐阅读