我在PHP中使用MySQL查询,但它在PHP中不起作用。但是当我在MySQL中运行它时,它运行良好。
在这个查询中,我用另一个查询的结果减去sum(EventData.odometerKM)的结果。
虽然我没有使用限制,但它返回“子查询返回超过1行” 我希望所有人都不受限制。
此处它以限制1返回:
Array
(
[0] =>
[licensePlate] =>
[1] => 15745455.854492635
[sum(EventData.odometerKM) - (SELECT sum(EventData.odometerKM) from Device LEFT JOIN EventData on Device.deviceID=EventData.deviceID where DATE_FORMAT(FROM_UNIXTIME(`timestamp`), '%d-%m-%Y') = '27-06-2017' AND Device.isActive=1 group by Device.licensePlat] => 15745455.854492635
)
代码:
$sqll = "SELECT Device.licensePlate, sum(EventData.odometerKM) - (SELECT sum(EventData.odometerKM) from Device LEFT JOIN EventData on Device.deviceID=EventData.deviceID where DATE_FORMAT(FROM_UNIXTIME(`timestamp`), '%d-%m-%Y') = '27-06-2017' AND Device.isActive=1 group by Device.licensePlate ) from Device LEFT JOIN EventData on Device.deviceID=EventData.deviceID where DATE_FORMAT(FROM_UNIXTIME(`timestamp`), '%d-%m-%Y') = '28-06-2017' AND Device.isActive=1 group by Device.licensePlate ";
$result2 = mysqli_query($conn, $sqll)or die("MySQL error: " . mysqli_error($conn) . "<hr>\nQuery: $query");;
$count=1;
while ($rows = mysqli_fetch_array($result2)){
'.$rows['sum(EventData.odometerKM)'].'<br>';
//$count++;
echo '<pre>';
print_r($rows);
}