简体   繁体   中英

PHP MSSQL Query inside Query

Day 3...

Here is the code and then I'll explain the task:

$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";
    }
  }

The issue is, I need the outer query to run, to narrow the results specifically, and then run the second query within, I think I may be going about it all wrong but when I've been looking at it for three days now wondering what is happening, I need outside help, and a referral to an asylum. What actually happens here is the outer query runs, then the inner query doesn't run on the narrows data using the data from the outer query. As it should by that point only have 1 result, being the most recent... I don't know whether or not this is the best way to do this or not, I did also look at storing it to an array but again it didn't do it as expected, all ideas welcome on this one.

Note: I only started working with MSSQL about two weeks ago, so give me a little credit ;)

Any and all help is greatly appreciated on this one!

Do the fact you are select just one row from the first query and you seems select the same row with the same $meterdID from the secondo query this mean that all the info you need could be retrieved in the first query (eventually add the missing column is select) then you could simply select and build you $data without a while loop but just checking if you have a result eg:

  $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";
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM