gpt4 book ai didi

mysql - 分组依据和日期

转载 作者:行者123 更新时间:2023-11-29 07:06:31 25 4
gpt4 key购买 nike

我正在尝试获取每天的预订量。我已经完成了这个 Doctrine 请求:

   // prepare date for request
$today = new DateTime('now');
$lastMonth = new DateTime('-30 days');

$qb
->select('r.reservationDate, sum(r.totalAmountNoTaxes) as totalAmountNoTaxes')
->where('r.companyId = :companyId')
->setParameter('companyId', $companyId)
->andWhere('r.reservationDate BETWEEN :month AND :today')
->setParameter('month', $lastMonth->format('Y-m-d'))
->setParameter('today', $today->format('Y-m-d'))
->orderBy('r.reservationDate', 'ASC')
->groupBy('r.reservationDate')
;

我得到了这样的结果(从我的网络服务)

 {
"reservationDate": "2016-12-21T07:41:12+0000",
"totalAmountNoTaxes": "26150.30"
},
{
"reservationDate": "2016-12-22T07:41:01+0000",
"totalAmountNoTaxes": "230.00"
},
{
"reservationDate": "2016-12-22T07:41:02+0000",
"totalAmountNoTaxes": "21966.40"
},
{
"reservationDate": "2016-12-23T07:40:52+0000",
"totalAmountNoTaxes": "20407.05"
},
{
"reservationDate": "2016-12-24T07:40:41+0000",
"totalAmountNoTaxes": "4319.75"
},
{
"reservationDate": "2016-12-24T07:40:42+0000",
"totalAmountNoTaxes": "7261.20"
},
{
"reservationDate": "2016-12-25T07:40:28+0000",
"totalAmountNoTaxes": "14821.45"
},
{
"reservationDate": "2016-12-26T07:40:16+0000",
"totalAmountNoTaxes": "494.50"
},
{
"reservationDate": "2016-12-26T07:40:17+0000",
"totalAmountNoTaxes": "14541.25"
},
{
"reservationDate": "2016-12-27T07:06:40+0000",
"totalAmountNoTaxes": "25763.90"
},
{
"reservationDate": "2016-12-28T05:37:43+0000",
"totalAmountNoTaxes": "20595.70"
}

问题是有时日期分组不正确。例如,2016-12-22 被分为 2。24、26 也是如此。

我该怎么办?

最佳答案

看起来您的数据库中有日期时间字段。解决方案是按日期格式分组:

//
->groupBy('DATE_FORMAT(r.reservationDate, '%Y%m%d')')
//

但是DATE_FORMAT未在默认 Doctrine 包中实现,因此您必须添加 beberlei/DoctrineExtensions然后将相应的函数添加到您的 Doctrine 捆绑配置中,例如:

doctrine:
dbal:
....
orm:
....
dql:
string_functions:
DATE_FORMAT: DoctrineExtensions\Query\Mysql\DateFormat

关于mysql - 分组依据和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41392795/

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