如何从毫秒列按月对 MYSQL 数据库的结果进行分组?
How to Group result from MSQL db by month from a milliseconds column?
我正在尝试使用 SLIM 框架从 MSQL 数据库中获取基于月份的项目总数,当我显示数据时,我得到了结果但分开了,即使在同一个月内。我的代码是,
$app->get('/view/test', function ($request, $response, array $args) {
try {
$con = $this->db;
date_default_timezone_set('UTC+3h');
$sql = "SELECT start_time,COUNT(*) as tt FROM parking_sessions GROUP
BY FROM_UNIXTIME(start_time/1000)";
$result = null;
$results = null;
foreach ($con->query($sql) as $row) {
$result['muda'] = date('M',($row['start_time']/1000));
$result['total'] = $row['tt'];
$results[] =$result;
$total = count($results);
}
if ($result) {
return $response->withJson(array('status' => 'true', 'date' =>
$results, 'jumla' => $total), 200);
} else {
return $response->withJson(array('status' => 'Owner Not Found'),
422);
}
} catch (\Exception $ex) {
return $response->withJson(array('error' => $ex->getMessage()), 422);
}
});
它给了我以下结果
"date": [
{
"month": "Feb",
"total": "1"
},
{
"month": "Mar",
"total": "1"
},
{
"month": "Apr",
"total": "1"
},
{
"month": "Apr",
"total": "1"
}
],
所有需要的结果如下
"date": [
{
"month": "Feb",
"total": "1"
},
{
"month": "Mar",
"total": "1"
},
{
"month": "Apr",
"total": "2"
}
],
我的数据库看起来像
id slot_id customer_vehicle_id start_time end_time check_out_time status
1 1 2 1553249524000 1554346841515 1553111000 1
2 6 4 1554351619803 1553253124000 1553214094 0
3 6 4 1555224715000 1553253124000 1553214094 0
4 7 3 5086800000 1553253124000 1553214094 0
您不能使用 unixtime 按查询分组,因为您在 unix 时间中每隔一秒和一分钟获取一次。您需要的是在没有时间的情况下将其转换为日期。
SELECT start_time,COUNT(*) as tt FROM parking_sessions GROUP
按日期 (FROM_UNIXTIME(start_time/1000))
注意:日期将截断分钟,您将只剩下日期
我正在尝试使用 SLIM 框架从 MSQL 数据库中获取基于月份的项目总数,当我显示数据时,我得到了结果但分开了,即使在同一个月内。我的代码是,
$app->get('/view/test', function ($request, $response, array $args) {
try {
$con = $this->db;
date_default_timezone_set('UTC+3h');
$sql = "SELECT start_time,COUNT(*) as tt FROM parking_sessions GROUP
BY FROM_UNIXTIME(start_time/1000)";
$result = null;
$results = null;
foreach ($con->query($sql) as $row) {
$result['muda'] = date('M',($row['start_time']/1000));
$result['total'] = $row['tt'];
$results[] =$result;
$total = count($results);
}
if ($result) {
return $response->withJson(array('status' => 'true', 'date' =>
$results, 'jumla' => $total), 200);
} else {
return $response->withJson(array('status' => 'Owner Not Found'),
422);
}
} catch (\Exception $ex) {
return $response->withJson(array('error' => $ex->getMessage()), 422);
}
});
它给了我以下结果
"date": [
{
"month": "Feb",
"total": "1"
},
{
"month": "Mar",
"total": "1"
},
{
"month": "Apr",
"total": "1"
},
{
"month": "Apr",
"total": "1"
}
],
所有需要的结果如下
"date": [
{
"month": "Feb",
"total": "1"
},
{
"month": "Mar",
"total": "1"
},
{
"month": "Apr",
"total": "2"
}
],
我的数据库看起来像
id slot_id customer_vehicle_id start_time end_time check_out_time status
1 1 2 1553249524000 1554346841515 1553111000 1
2 6 4 1554351619803 1553253124000 1553214094 0
3 6 4 1555224715000 1553253124000 1553214094 0
4 7 3 5086800000 1553253124000 1553214094 0
您不能使用 unixtime 按查询分组,因为您在 unix 时间中每隔一秒和一分钟获取一次。您需要的是在没有时间的情况下将其转换为日期。 SELECT start_time,COUNT(*) as tt FROM parking_sessions GROUP 按日期 (FROM_UNIXTIME(start_time/1000)) 注意:日期将截断分钟,您将只剩下日期