sqlmysqldate

MYSQLi and Highcharts


I am having trouble with my MYSQL query. I can send it to Highcharts but is is not quite right. I want to get last months temperature and put it in Highcharts. My query sort of works but only gets the first temperature for each day, and that is just after midnight. How do I make a query that will get hourly temperatures daily for a month? This is my query. I am sorry I cannot get this formatting correct.

<?php
$con = mysqli_connect("localhost","root","root","manortsc_test");

if (!$con) {
die('Could not connect: ' . mysql_error());
}

$sth = mysqli_query($con,"
SELECT DateTime,T
FROM alldata
WHERE YEAR(DateTime) = YEAR(CURDATE()) AND MONTH(DateTime) = MONTH(CURDATE() - INTERVAL 1 MONTH)
                            GROUP BY YEAR(DateTime), MONTH(DateTime), DAY(DateTime)
"
);
$rows = array();
$rows['name'] = 'Outside';
while($r = mysqli_fetch_array($sth)) {
    $rows['data'][] = $r['T'];
}
$sth = mysqli_query($con,"
SELECT DateTime,Ti
FROM alldata
WHERE YEAR(DateTime) = YEAR(CURDATE()) AND MONTH(DateTime) = MONTH(CURDATE() - INTERVAL 1 MONTH)
                            GROUP BY YEAR(DateTime), MONTH(DateTime), DAY(DateTime)
"
);
$rows1 = array();
$rows1['name'] = 'Inside';
while($rr = mysqli_fetch_assoc($sth)) {
    $rows1['data'][] = $rr['Ti'];
}
$result = array();
array_push($result,$rows);
array_push($result,$rows1);

print json_encode($result, JSON_NUMERIC_CHECK);

mysqli_close($con);
?>

Solution

  • Spent all night researching and came up with the answer. It simply gets the maximum T each day for the month. I can build on this. Hope it helps someone.

    SELECT DateTime,max(T)
    FROM alldata
    WHERE YEAR(DateTime) = YEAR(CURDATE()) AND MONTH(DateTime) = MONTH(CURDATE() - INTERVAL 1 MONTH)
    GROUP BY day(DateTime)
    
    "