【发布时间】:2021-01-15 18:13:25
【问题描述】:
你好我的朋友我有一个问题。我正在查询并希望按天获取记录组。现在我已经完成了这段代码:
$pedidosChart = DB::table('pedidos')
->selectRaw(' count(*) as count , DATE_FORMAT(fecha_creado, \'%d\') as day , sum( pedidos.precio_total) as amount ')
->whereMonth('fecha_creado', 12)
->groupBy('day')
->get()
我知道了:
0 => {#1388 ▼
+"count": 4
+"day": "15"
+"amount": 6073.5798950195
}
1 => {#1387 ▼
+"count": 8
+"day": "17"
+"amount": 22565.599243164
}
2 => {#1389 ▼
+"count": 11
+"day": "18"
+"amount": 13111.219726562
}
我想得到:
[▼
15 => array:2 [▼
"count" => 4
"amount" => 6073.5798950195
]
17 => array:2 [▼
"count" => 8
"amount" => 22565.599243164
]
18 => array:2 [▼
"count" => 11
"amount" => 13111.219726562
]
21 => array:2 [▼
"count" => 4
"amount" => 2910.5600585938
]
我得到了上述结果,因为使用 foreach(循环)对数据进行排序。 有可能吗?怎么样?
【问题讨论】:
-
不完整的细节关键 15,17,18 他们来自哪里?
-
它们是创建记录时每条记录的天数->检查选择
-
15、17、18、21 是必需的结果吗?
-
仅供参考:您在这里没有使用 Eloquent,仅使用查询构建器。尝试将其作为纯 SQL 运行 - 您可能会得到更清晰的消息。顺便说一句,所有非聚合列都应该在 group by ...
标签: php mysql laravel eloquent