第3天......
这是代码,然后我将解释任务:
$outerquery = "SELECT TOP 1 D2001_SPID, D3001_MeterId as MeterID, D3009_MeterReadDate, D3008_MeterRead, D3010_MeterReadType, customer_id, reading_by FROM rc_meter_reads WHERE (D3001_MeterId IN (SELECT water_meter_id FROM rc_meters WHERE (status = '1'))) OR (D3001_MeterId IN (SELECT waste_meter_id FROM rc_meters AS rc_meters_1 WHERE (status = '1'))) AND (D3010_MeterReadType = 'C') ORDER BY D3009_MeterReadDate DESC";
$rs6 = sqlsrv_query($conn,$outerquery);
while($outerrow = sqlsrv_fetch_array($rs6)) {
$meterID = $outerrow['MeterID'];
$query7 = "SELECT TOP 1 * from rc_meter_reads where D3001_MeterId = '" . $meterID . "'";// (SELECT MAX(D3009_MeterReadDate) from rc_meter_reads) ORDER BY D3009_MeterReadDate DESC";
$rs7 = sqlsrv_query($conn1,$query7);
while($row = sqlsrv_fetch_array($rs7))
{
if(strtotime($nextreaddue) >= strtotime($today)) {
//$data.= $row['customer_id'] . "," . $row['D2001_SPID'] . "," . $row['D3001_MeterId'] . "," . $row['D3008_MeterRead'] . "," . $meterreaddate . "," . $lastreadadd196 . "</br>\n";
}
}
问题是,我需要运行外部查询,专门缩小结果,然后在其中运行第二个查询,我想我可能会发生错误,但是当我一直在查看它时几天现在想知道发生了什么,我需要外面的帮助,并转介到庇护。这里实际发生的是外部查询运行,然后内部查询不使用外部查询中的数据在窄数据上运行。因为它应该只有1个结果,是最近的...我不知道这是否是最好的方式来做到这一点,我也看着将它存储到一个数组,但它再次没有像预期的那样做,所有的想法都欢迎这一点。
注意:我大约两周前才开始使用MSSQL,所以给我一点功劳;)
非常感谢任何和所有帮助!
答案 0 :(得分:0)
你从第一个查询中选择了一行,你似乎从secondo查询中选择了同一行具有相同的$ meterdID 这意味着您可以在第一个查询中检索所需的所有信息(最终添加缺少的列是select) 那么你可以简单地选择并构建你没有while循环的数据,但只是检查你是否有结果,例如:
$outerquery = "SELECT TOP 1
D2001_SPID
, D3001_MeterId as MeterID
, D3009_MeterReadDate
, D3008_MeterRead
, D3010_MeterReadType
, customer_id
, reading_by
FROM rc_meter_reads
WHERE (D3001_MeterId IN (
SELECT water_meter_id
FROM rc_meters WHERE (status = '1')
))
OR (D3001_MeterId IN (
SELECT waste_meter_id
FROM rc_meters AS rc_meters_1 WHERE (status = '1')
))
AND (D3010_MeterReadType = 'C')
ORDER BY D3009_MeterReadDate DESC";
if ($row = sqlsrv_fetch_array($rs6)){
$data.= $row['customer_id'] . "," .
$row['D2001_SPID'] . "," .
$row['D3001_MeterId'] . "," .
$row['D3008_MeterRead'] . "," .
$meterreaddate . "," .
$lastreadadd196 . "</br>\n";
}