我是 PHP 和 SQL 查询的新手,但正在努力学习。无论如何,我有以下代码,我相信会有一种更简单的方法(希望我打算在一年中的每一天而不是一个月中做同样的事情)。
我当前的代码是;
//Get jan power consumption
$janquery = mysql_query("SELECT SUM(kw*tdsecs) FROM data WHERE year(time) = year(CURDATE()) AND month(time) = 1");
$janresult = mysql_fetch_array($janquery);
$jankwm = $janresult[0] / 60;
$jankwh = round($jankwm / 60,2);
//Get feb power consumption
$febquery = mysql_query("SELECT SUM(kw*tdsecs) FROM data WHERE year(time) = year(CURDATE()) AND month(time) = 2");
$febresult = mysql_fetch_array($febquery);
$febkwm = $febresult[0] / 60;
$febkwh = round($febkwm / 60,2);
以此类推……
$values=array(
"Jan" => $jankwh,
"Feb" => $febkwh,
"Mar" => $markwh,
"Apr" => $aprkwh,
"May" => $maykwh,
"Jun" => $junkwh,
"Jul" => $julkwh,
"Aug" => $augkwh,
"Sep" => $sepkwh,
"Oct" => $octkwh,
"Nov" => $novkwh,
"Dec" => $deckwh
);
我想避免有 12 个查询,所以任何帮助将不胜感激。