-1
我想根据与两列中每一列相关的位置返回两列的总数。 当我使用OR语句像这样:SQL语句不计算SUM和多个WHERE子句
$query = "SELECT ec.ElecEnergy, ec.GasEnergy, ua.City,
SUM(ec.ElecEnergy) AS ecTotal,
SUM(ec.GasEnergy) AS gcTotal
FROM energyconsumption ec INNER JOIN
useraccount ua
ON ec.User = ua.id
WHERE ua.City ='London'
OR City ='Manchester'
OR City ='Luton'
OR City ='Newcastle'
OR City ='Birmingham'
OR City ='Blackburn'
GROUP BY ec.ElecEnergy, ec.GasEnergy, ua.City
ORDER BY ec.ID ASC";
$result = mysqli_query($conn,$query);
$r = array();
if($result->num_rows){
while($row = mysqli_fetch_array($result)){
array_push($r, array('ElecConsump' => $row['ecTotal'],'GasConsump' =>$row['gcTotal'],
'Location' => $row['City']
));
}
} echo json_encode(array('results' => $r));
它只是返回一切从我的数据库。我想要的是根据列出的每个城市列的总和。
我不知道我在做什么错
在此先感谢!
您正在返回实际金额以及SUM!这可以防止服务器聚合这些值。您还应该添加“GROUP BY ua.City” – stuartd
不确定这是否有问题,但您是否缺少城市的前缀? (市vs ua.City) –