php - 获取表中跨列的记录的总大小 - symfony

标签 php mysql symfony

我在 mysql 中有一个列,我需要在我的 symfony 应用程序的列中检索表中总记录的大小。

这是表的结构,我想在其中获取同意列中记录的总大小,因此在本例中它应该返回 2:

id | agrees | disjoint |
1  |  1     | 0
2  |  1     | null

下面是我的方法,但它只是从表中获取所有内容,但我无法计算同意列的大小

$restresults = $this->getDoctrine()->getRepository('xxxBundle:Entity')->findAll();

      $data = array();
        foreach ($restresults as $restresult) {

            array_push($data, $this->serialize($restresult));

        }

     $response = new Response(json_encode($data), 200);
     $response->headers->set('Content-Type', 'application/json');

请问如何获取列中记录的总大小?

最佳答案

如果您需要计算表中的元素,请使用:

$qb = $entityManager->createQueryBuilder();
$qb->select('count(e.id)');
$qb->from('xxxBundle:Entity','e');

$count = $qb->getQuery()->getSingleScalarResult();

如果您需要总和列,请使用

$qb->select('sum(e.agrees)');

更多信息请参见 http://docs.doctrine-project.org/en/latest/reference/query-builder.html

关于php - 获取表中跨列的记录的总大小 - symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47530492/

相关文章:

php - 如何在 Bisna 中正确加载 DoctrineExtensions?

MYSQL - 全文匹配失败 'NA'

mysql - 查询显示两个表中的 0 条记录在连接时具有相同的数据

symfony - 在链配置的命名空间中找不到另一个 "The class ' X'

php - PHP中的进程间通信

PHP MySQL 数据库

PHP SOAP 过程 'functionName' 不存在

symfony - 使用 .env 动态禁用 Symfony 4 中的网络工具栏和分析器

php - 我如何在 OSX 10.11.6 上使用 OpenSSL 而不是 SecureTransport 和 Apache PHP

jquery - 当 ajax 请求被中止时,我如何关闭 mysql 连接?