插入到临时表中,然后在 while 循环中从临时表查询中选择



我有一个包含两个sql查询的PHP循环。一种是插入temp_Table。另一个是 从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 语句应在 INSERT 完成后运行一次。目前,它为每个插入执行。

只需将其从 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);

最新更新