Insert into tempTable and Select From tempTable queries inside while 循环

Insert into tempTable and Select From tempTable queries inside while loop

我有一个 PHP 循环,其中包含两个 sql 查询。一个是 INSERT INTO a temp_Table。另一个,SELECT * FROM temp_Table。 SELECT 语句执行了多次,我的结果不正确,因为有重复。有没有办法在所有 INSERT INTO 语句执行完毕后只执行一次 SELECT 语句?

while(odbc_fetch_row($gotdaysreports)){

 $insertQuery = "insert into temp_Table (
   REPORTNR,
   ACCOUNTNO,
   ACCOUNTNAME,
   USERNAME,
   TOTALVALUE,
   TOTALTAX,
   DISCOUNT,
   DISCTAX,
   DISCOUNT )select  * from SUPPLIER_PURCHASES( $company_id,$branchindex,$regionindex,$dayendid ,$accountnumber)";

$selectQuery = "select * from temp_Table";  
}

$gotinsertQuery = odbc_exec($connect, $insertQuery);

$gotselectQuery = odbc_exec($connect, $selectQuery);

select 语句应该在 INSERTS 完成后 运行 一次。目前,它针对每个插入执行。

只需将它从 while 循环中取出,连接插入语句,它就会像魅力一样工作。

 while(odbc_fetch_row($gotdaysreports)){

 $insertQuery.= "insert into temp_Table (
   REPORTNR,
   ACCOUNTNO,
   ACCOUNTNAME,
   USERNAME,
   TOTALVALUE,
   TOTALTAX,
   DISCOUNT,
   DISCTAX,
   DISCOUNT )select  * from SUPPLIER_PURCHASES( $company_id,$branchindex,$regionindex,$dayendid ,$accountnumber);";

}
 $selectQuery = "select * from temp_Table"; 

$gotinsertQuery = odbc_exec($connect, $insertQuery);

$gotselectQuery = odbc_exec($connect, $selectQuery);