Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 条令-按id统计表中的用户数_Php_Symfony_Doctrine Orm_Query Builder - Fatal编程技术网

Php 条令-按id统计表中的用户数

Php 条令-按id统计表中的用户数,php,symfony,doctrine-orm,query-builder,Php,Symfony,Doctrine Orm,Query Builder,我在数据库中有6条记录。 3来自用户1,2来自用户2,1来自用户3 我想按用户id对它们进行计数,这样最终结果将是3。它总是返回表中所有记录的计数。它返回6 我的职责: return $this->getDeviceRepository() ->createQueryBuilder('tlu') ->select('COUNT(tlu.user_id)') ->getQuery() ->getResul

我在数据库中有6条记录。 3来自用户1,2来自用户2,1来自用户3

我想按用户id对它们进行计数,这样最终结果将是3。它总是返回表中所有记录的计数。它返回6

我的职责:

return $this->getDeviceRepository()
        ->createQueryBuilder('tlu')
        ->select('COUNT(tlu.user_id)')
        ->getQuery()
        ->getResult();
您有两种解决方案(使用groupBy或countDistinct)

您有两种解决方案(使用groupBy或countDistinct)


是的,我们可以这样做

getResult()->count();


是的,我们可以这样做

getResult()->count();


这回答了你的问题吗?这回答了你的问题吗?我需要在结果上添加count($result),因为它返回包含3个对象的数组@RachedBCHI需要在结果上添加count($result),因为它返回包含3个对象的数组@拉赫德伯奇
getResult()->count();
return $this->getDeviceRepository()
        ->createQueryBuilder('tlu')
        ->select('COUNT(tlu.user_id)')
        ->countDistinct('tlu.user_id')
        ->getQuery()
        ->getSingleScalarResult();