PHP/MySQL无法根据我当前的表对数据进行分组和打印
我有这个表,有一些样本数据PHP/MySQL无法根据我当前的表对数据进行分组和打印,php,mysql,amcharts,Php,Mysql,Amcharts,我有这个表,有一些样本数据 CREATE TABLE IF NOT EXISTS ooscount ( id int(10) NOT NULL AUTO_INCREMENT, agcid int(3) NOT NULL, ooscount int(10) NOT NULL, `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CH
CREATE TABLE IF NOT EXISTS ooscount (
id int(10) NOT NULL AUTO_INCREMENT,
agcid int(3) NOT NULL,
ooscount int(10) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=246045 ;
--
-- Dumping data for table 'ooscount'
--
INSERT INTO `ooscount` (`id`, `agcid`, `ooscount`, `date`) VALUES
(209855, 4, 2869, '2018-03-01 18:00:02'),
(209856, 2, 3183, '2018-03-01 18:00:02'),
(209857, 1, 4906, '2018-03-01 18:00:02'),
(209860, 3, 5596, '2018-03-01 18:00:02'),
(209863, 14, 6019, '2018-03-01 18:00:02'),
(209864, 16, 7564, '2018-03-01 18:00:02'),
(209873, 4, 2870, '2018-03-01 18:05:01'),
(209874, 2, 3182, '2018-03-01 18:05:01'),
(209876, 1, 4899, '2018-03-01 18:05:01'),
(209877, 3, 5598, '2018-03-01 18:05:01'),
(209879, 14, 6018, '2018-03-01 18:05:01'),
(209882, 16, 7557, '2018-03-01 18:05:01');
我正在为现有数据和新数据建立一个不同的图表/绘图系统,希望我不需要将存储方法更改为MySQL
我想要的结果是这样的:
第一个数字是agcid,第二个数字是ooscount。我每5分钟对这些数据进行一次采样,并希望它以5分钟的间隔绘制最后7小时的数据
"dataProvider": [
{
"1": 2055,
"2": 3845,
"3": 4455,
"4": 5051,
"14": 9012,
"16": 6522,
"date": "2018-03-08 02:45"
},
{
"1": 2077,
"2": 3841,
"3": 4450,
"4": 5055,
"14": 9033,
"16": 6524,
"date": "2018-03-08 02:50"
},
{
"1": 2076,
"2": 3821,
"3": 4452,
"4": 5057,
"14": 9064,
"16": 6525,
"date": "2018-03-08 02:55"
},
{
"1": 2071,
"2": 3814,
"3": 4460,
"4": 5059,
"14": 9011,
"16": 6521,
"date": "2018-03-08 03:00"
},
{
"1": 2064,
"2": 3832,
"3": 4490,
"4": 5052,
"14": 9013,
"16": 6496,
"date": "2018-03-08 03:05"
},
不过,我想不出实现这一目标的最佳方法
从上表中可以看出,查询中每个agcid的数据都是agcid:ooscount(目前为6)
使用AmCharts,目标是在一个图表上绘制这6个数据点。一种方法是在fetch_row循环期间在PHP中对行进行分组,例如(为了简洁起见,省略了错误检查): 另一种方法是对结果集进行透视,使每个id都是一列,并使用来自的技术按日期对其进行分组 在本例中,我使用动态版本来提取所有ID,但是如果您只需要特定的ID,可以相应地调整它。使用您提供的转储,这将为您提供以下结果集:
+---------------------+------+------+------+------+------+------+
| date | 4 | 2 | 1 | 3 | 14 | 16 |
+---------------------+------+------+------+------+------+------+
| 2018-03-01 18:00:02 | 2869 | 3183 | 4906 | 5596 | 6019 | 7564 |
| 2018-03-01 18:05:01 | 2870 | 3182 | 4899 | 5598 | 6018 | 7557 |
+---------------------+------+------+------+------+------+------+
您可以在脚本中使用mysqli::multi_query
将所有内容组合在一起,如下所示:
$sql = <<<SQL
SET @sql = NULL;
SELECT
GROUP_CONCAT(distinct
CONCAT(
'sum(case when agcid = ',
agcid,
' then ooscount else 0 end) AS ''',
replace(agcid, ' ', ''),
''''
)
) INTO @sql
from (select distinct agcid from ooscount) a;
SET @sql = CONCAT('SELECT `date`, ', @sql, ' from ooscount where `date` >= DATE_SUB( NOW( ) , INTERVAL 7 HOUR ) group by `date`');
PREPARE stmt from @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SQL;
$db->multi_query($sql);
do {
if ($result = $db->store_result()) {
$results = [];
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
}
} while ($db->more_results() && $db->next_result());
echo json_encode($results);
一种方法是在fetch_row循环期间在PHP中对行进行分组,例如(为了简洁起见,省略了错误检查): 另一种方法是对结果集进行透视,使每个id都是一列,并使用来自的技术按日期对其进行分组 在本例中,我使用动态版本来提取所有ID,但是如果您只需要特定的ID,可以相应地调整它。使用您提供的转储,这将为您提供以下结果集:
+---------------------+------+------+------+------+------+------+
| date | 4 | 2 | 1 | 3 | 14 | 16 |
+---------------------+------+------+------+------+------+------+
| 2018-03-01 18:00:02 | 2869 | 3183 | 4906 | 5596 | 6019 | 7564 |
| 2018-03-01 18:05:01 | 2870 | 3182 | 4899 | 5598 | 6018 | 7557 |
+---------------------+------+------+------+------+------+------+
您可以在脚本中使用mysqli::multi_query
将所有内容组合在一起,如下所示:
$sql = <<<SQL
SET @sql = NULL;
SELECT
GROUP_CONCAT(distinct
CONCAT(
'sum(case when agcid = ',
agcid,
' then ooscount else 0 end) AS ''',
replace(agcid, ' ', ''),
''''
)
) INTO @sql
from (select distinct agcid from ooscount) a;
SET @sql = CONCAT('SELECT `date`, ', @sql, ' from ooscount where `date` >= DATE_SUB( NOW( ) , INTERVAL 7 HOUR ) group by `date`');
PREPARE stmt from @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SQL;
$db->multi_query($sql);
do {
if ($result = $db->store_result()) {
$results = [];
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
}
} while ($db->more_results() && $db->next_result());
echo json_encode($results);
ooscont
是一个合计还是最后一条记录?您的查询将返回6行吗?ConfusedIt将是设备agcid的最后一次插入计数。agcid绑定到具有脱机子设备计数(ooscount)的设备。我还意识到这个表不需要实际的ID列?谢谢,我已经更新了样本数据并澄清了一点。ooscont
是一个合计还是最后一个记录?您的查询将返回6行吗?ConfusedIt将是设备agcid的最后一次插入计数。agcid绑定到具有脱机子设备计数(ooscount)的设备。我还意识到这个表不需要实际的ID列?谢谢,我已经更新了样本数据并澄清了一点。我会处理这个,看起来正是我想要的。非常感谢。我会努力的,看起来正是我想要的。非常感谢。
$sql = <<<SQL
SET @sql = NULL;
SELECT
GROUP_CONCAT(distinct
CONCAT(
'sum(case when agcid = ',
agcid,
' then ooscount else 0 end) AS ''',
replace(agcid, ' ', ''),
''''
)
) INTO @sql
from (select distinct agcid from ooscount) a;
SET @sql = CONCAT('SELECT `date`, ', @sql, ' from ooscount where `date` >= DATE_SUB( NOW( ) , INTERVAL 7 HOUR ) group by `date`');
PREPARE stmt from @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SQL;
$db->multi_query($sql);
do {
if ($result = $db->store_result()) {
$results = [];
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
}
} while ($db->more_results() && $db->next_result());
echo json_encode($results);
[{
"date": "2018-03-01 18:00:02",
"4": "2869",
"2": "3183",
"1": "4906",
"3": "5596",
"14": "6019",
"16": "7564"
}, {
"date": "2018-03-01 18:05:01",
"4": "2870",
"2": "3182",
"1": "4899",
"3": "5598",
"14": "6018",
"16": "7557"
}]