我正在寻找按天对我的政策进行分组的方法。我尝试了很多示例如何执行此操作,但仍然存在一些错误。谁能帮我这个?在这里,我将只展示两个示例,我尝试的其他示例与这些示例相似。差异仅在于使用的 SQL 函数 ex(CAST, SUBSTRING, DATE...) 我尝试的第一种方法是:
$query = $this->getEntityManager()
->createQueryBuilder();
$query->select('count(p), p.transactionDate')
->from('GLPolicyBundle:Policy', 'p')
->andwhere('p.shop IN (:shop_id)')
->setParameter('shop_id', $shop_list)
->andWhere($query->expr()->between('p.transactionDate', ':date_from', ':date_to'))
->setParameter('date_from', $date_from, \Doctrine\DBAL\Types\Type::DATETIME)
->setParameter('date_to', $date_to, \Doctrine\DBAL\Types\Type::DATETIME)
->addGroupBy('DAY(p.transactionDate)');
getDQL() 返回:
SELECT count(p), p.transactionDate FROM GLPolicyBundle:Policy p
WHERE p.shop IN (:shop_id) AND (p.transactionDate BETWEEN :date_from AND :date_to)
GROUP BY DAY(p.transactionDate)
错误是:
[语义错误] 第 0 行,第 156 列靠近 'DAY(p.transa':错误:无法按未定义的标识或结果变量分组。
第二种方法是:
$query = $this->getEntityManager()
->createQuery("SELECT p, (p.transactionDate) AS group
FROM GLPolicyBundle:Policy p
WHERE p.shop IN (:shop_id) AND (p.transactionDate BETWEEN :date_from AND :date_to)
GROUP BY DAY(group)")
->setParameter('shop_id', $shop_list)
->setParameter('date_from', $date_from, \Doctrine\DBAL\Types\Type::DATETIME)
->setParameter('date_to', $date_to, \Doctrine\DBAL\Types\Type::DATETIME);
getDQL() 返回:
SELECT p, (p.transactionDate) AS group FROM GLPolicyBundle:Policy p
WHERE p.shop IN (:shop_id) AND (p.transactionDate BETWEEN :date_from AND :date_to)
GROUP BY DAY(group)
错误是:
[语义错误] 第 0 行,第 72 列,靠近“FROM GLPolicyBundle:Policy”:错误:未定义“FROM”类。