I'm new with CakePHP 3 and I want to know how I can use a DISTINCT inside a COUNT such as the following SQL query.
SELECT `column_one`, COUNT(DISTINCT `column_two`) FROM `table` GROUP BY `column_one`
you can simply do
$query = $table->find()
->select([
'column_one',
'count' => "COUNT(DISTINCT `column_two`)"
])
->group(['column_one']);
but if you want to use cakephp sql function you can do
...
'count' => $query->func()->count('DISTINCT `column_two`')
....
$query needs to be instantiated separately. Like so: $query = $table->find('all'); $query = $query->select([ 'column_one', 'count' => $query->func()->count('DISTINCT column_two')]);