gpt4 book ai didi

php - 学说 2 选择计数 groupBy

转载 作者:可可西里 更新时间:2023-10-31 23:30:47 25 4
gpt4 key购买 nike

我正在尝试检索具有唯一 uid 的许多行。

$qb->select('COUNT() as cnt')
->from($type, 'c')
->groupBy('c.organization, c.process_role, c.domain, c.year')
->getQuery()->getSingleScalarResult()

但它返回一组组计数。这个应该怎么写才正确?

终于成功了,但是有点丑

$count = $this->_em->createQuery( 'SELECT COUNT(c.id) FROM '.$type.' as c WHERE c.id IN ('
. 'SELECT c1.id FROM ' . $type . ' c1 '
. 'GROUP BY c1.organization, c1.process_role, c1.domain, c1.year)')
->getSingleScalarResult();

最佳答案

尝试将单个 groupBy 分解为 addGroupBy 函数:

$qb->select('COUNT(c) as cnt')
->from($type, 'c')
->groupBy('c.organization')
->addGroupBy('c.process_role')
->addGroupBy('c.domain')
->addGroupBy('c.year')
->getQuery()->getSingleScalarResult();

然而,这实际上返回相同的东西,因为它只是将结果按这 4 个变量分组到唯一的集合中。您应该改为使用 DISTINCT 选择方法并对结果行进行计数。

$rows = $this->getDoctrine()->getManager()
->createQuery(
'SELECT DISTINCT c.organization, c.process_role, c.domain, c.year FROM ' . $type . ' c'
)
->getArrayResult();
$count = count($rows);

这是我已知的唯一可行的方法,因为 DQL 不支持任何替代策略(例如 SELECT COUNT(*) FROM (SELECT DISTINCT ... ))

关于php - 学说 2 选择计数 groupBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26656681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com