LET尝试使其尽可能简单。我正在尝试从MySQL Server获取传感器记录。有10个传感器将其值编译到我的DB中的桌子上。现在,为了试图使这更容易,只要说3天的记录让我们从2025年1月1日开始,因为这是一件奇怪的事情,就像在此示例中发送到Google图表时:
sensorID | sensorValue | sensorDate
1 | 394 | 2025-01-01 00:01:00 (time is irrelevant as explained below)
2 | 432 | 2025-01-01 00:01:10
3 | 434 | 2025-01-01 00:01:15
1 | 405 | 2025-01-02 00:01:00
2 | 416 | 2025-01-02 00:01:10
3 | 328 | 2025-01-02 00:01:15
2 | 428 | 2025-01-03 00:01:10 <--- Yes this can happen if a sensor didn't respond on first check
1 | 421 | 2025-01-03 00:01:15 ----- However the YYYY-M-D is all I'm concerned about with ID.
3 | 301 | 2025-01-03 00:01:20 ----- Below the correct order is placed in the build of the array.
现在我将数据提取以下查询:
$result0 = mysqli_query($conn, "SELECT DISTINCT sensorID, DATE_FORMAT(sensorDate, '%Y-%m-%d') as sensorDate_change, sensorValue FROM sensorRecords ORDER BY sensorDate, sensorID") ;
上面我认为将按日期和ID保持秩序。但是无论如何都解决了真正的问题。
我需要做的是构建数据。图表。现在,基于Google Wacky编号这个月的古怪方式,这没什么大不了的,我只能将Y-M-D值提取到1月以来的一个月。是的,这很奇怪现在我需要做的是提取一个日期的值(对于此示例为3天)并将其格式化为数据。
[new Date(2025, 0, 1), 394, 432, 434], [new Date(2025, 0, 2), 405, 416, 328], [new Date(2055, 0, 3), 421, 428, 301]
update:
我想到的代码是,但是,我有一个新问题。它为3个记录中的每一个重复了3次。我是如此近,很疯狂,使它变得正确工作。 代码:
<?php
// include Global PHP
include_once 'global.php' ;
// include MySQL Database Configuration File
include_once 'rcf_db.php' ;
// Begin Google Chart Rows <<<<<<<<<<
echo 'data.addRows([ ' ;
// Grab Data from Sensor Records
$result0 = mysqli_query($conn2, "SELECT DISTINCT DATE_FORMAT(sensorDate, '%Y-%m-%d') as date_change, sensorDate FROM sensorRecords ORDER BY sensorDate") ;
while($row = mysqli_fetch_array($result0)) {
$recordDate = $row['date_change'] ;
$sensorDate = $row['sensorDate'] ;
$dateValue = strtotime($recordDate) ;
$year = date('Y',$dateValue) ;
$monthNo = date('m',$dateValue) ;
$monthNo = $monthNo - 1 ;
$monthDay = date('d',$dateValue) ;
// Build Google Chart Rows Part 1
$build_part_1 = '[new Date('.$year.', '.$monthNo.', '.$monthDay.'),' ;
// Build Google Chart Rows Part 2
$SQL1 = "SELECT sensorValue FROM sensorRecords WHERE DATE_FORMAT(sensorDate, '%Y-%m-%d') = '".$recordDate."' ORDER BY sensorID;";
$result1 = mysqli_query($conn2, $SQL1) ;
// Do for Each Matching Date
if(mysqli_num_rows($result1) > 1) {
$strings = [] ;
foreach($result1 as $row) {
$strings[] = $row['sensorValue'] ;
}
}
// Echo Results
$build_part_2 = implode(', ', $strings).'], ' ;
echo $build_part_1.' '.$build_part_2.'' ;
}
// Close Google Chart Rows
echo ']);' ;
// Free Result Set
mysqli_free_result($result0) ;
mysqli_free_result($result1) ;
// Close DB Connections
$conn2->close() ;
?>
结果::(
data.addRows([ [new Date(2025, 0, 01), 394, 432, 434], [new Date(2025, 0, 01), 394, 432, 434], [new Date(2025, 0, 01), 394, 432, 434], [new Date(2025, 0, 02), 405, 416, 328], [new Date(2025, 0, 02), 405, 416, 328], [new Date(2025, 0, 02), 405, 416, 328], [new Date(2025, 0, 03), 421, 428, 301], [new Date(2025, 0, 03), 421, 428, 301], [new Date(2025, 0, 03), 421, 428, 301], ]);
显然,我只希望每个数据日期仅出现一次,而不是3次。最近有时间在我的自定义家庭自动化系统中投入更多工作,并想添加一些设备的指标。
任何帮助如何构建最后一行的人都将不胜感激。它是如此近....
示例
SELECT DISTINCT DATE_FORMAT(sensorDate, '%Y-%m-%d') as date_change, sensorDate FROM sensorRecords
, sensorDate
DISTINCT
适用于整个结果行(不仅是接下来的字段),因此,如果您在一天 +全时间戳记直到几秒钟,则不同的秒数将在同一天返回3个不同的结果。这就是为什么您每天循环3次。