我试图从我的SQLite表中选择所选月份中的所有行,并对“总计”列中的值求和。
这是我的示例表:
| ID | DATE | TOTAL |
| 1 | 22-11-2017 | 700 |
| 2 | 26-11-2017 | 100 |
| 3 | 28-11-2017 | 150 |
| 4 | 30-11-2017 | 50 |
这是dql函数
public function getDataByMonth($repo, $year, $month)
{
date = new \DateTime("{$year}-{$month}-01 00:00");
$dateEnd = new \DateTime("{$year}-{$month}-31 23:59");
$query = $qb->where('b.date BETWEEN :start AND :end')
->setParameter('start', $date->format('d-m-Y H:i'))
->setParameter('end', $dateEnd->format('d-m-Y H:i'))
->select('SUM(b.total) as totals');
}
即使查询成功,当我将
$month
变量设置为October(so 10)时,查询也会返回1000
而不是NULL
。我正在使用
$earnings = $this->getDoctrine()->getManager()->getRepository(Invoices::class);
$this->getDataByMonth($earnings,date("Y"), strftime("%m", strtotime("-1 months")));
我做错了什么?
我需要选择与所选月份相关的正确行。
最佳答案
也许您应该尝试像这样,遇到类似的问题并且它起作用了:
->add('where', $qb->expr()->between(
'e.datetimefield',
':from',
':to'
))
它是独立于数据库软件的,应可与大多数数据库一起使用,更多信息请参见准则文档:
http://docs.doctrine-project.org/en/latest/reference/query-builder.html#the-expr-class
也不格式化日期时间对象,例如:
$from = new \DateTime();
->setParameter('from', $from);
关于php - Symfony:DQL在日期之间选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47559756/