大型MySQL查询和插入

时间:2010-07-29 13:26:48

标签: php mysql

嘿大家我刚刚来到这里以及php只用了大约2个月就有点学习了。我现在需要做的是从数据库中的大约16个不同的表中获取数据并复制表。该表适用于申请人,并且要么具有共同的应用程序ID,要么具有共同的家庭ID,id是刚刚生成的哈希值。我现在正在做的是单独擦除每个表,然后使用数据进行插入,只需更改应用程序ID和/或id。有些桌子非常大,我试图想出一个更容易更快的方法来做这个,这是我为我的一张桌子写的一个例子。

 <?
      //get family info based on application id
 $queryC="SELECT Fam_Type, Fam_Title, Fam_SSNI20, Fam_SSNI20_Select, Fam_Name_First, Fam_Name_Middle, Fam_Name_Last, Fam_Name_Suffix, Fam_Gender, Fam_DOB, Fam_Disabled, Fam_Addy1, Fam_Addy2, Fam_City, Fam_State_Prov, Fam_Zip, Fam_Country, Fam_Home_Phone, Fam_Work_Phone, Fam_Work_Phone_Ext, Fam_Cell_Phone, Fam_Occupation, Fam_Employer, Fam_Primary_Res, Fam_Custody, Fam_Guard_Excuse, Fam_ChiDep_Ans, Fam_ReaVeh_Ans, InstructDone, SetDone, AppDone, HouDone, DepDone, AssDone, ReaDone, IncDone, ExpDone, QueDone, ApplicationDone, StatusDone,  Fam_Init, Fam_Start_Date, Fam_End_Date, Fam_Agree, username, password, email, Fam_Letter, Exp_Educational, Fam_Tax, Scan_FileNames, Scan_Descriptions, Fam_Complete, referral, Fam_Holds, appealed, Appeal_Letter, Appeal_Viewed, taxDocuments        
     FROM familY
     WHERE familyID='$famid' AND applicationID='$appid'";
     $resultC = mysql_db_query($aidDB, $queryC, $connection); echo $queryC;
     while($rC=mysql_fetch_array($resultC)){
       $Fam_Type="$rC['0']";
       $Fam_Title="$rC['1']";
       $Fam_SSNI20="$rC['2']";
       $Fam_SSNI20_Select="$rC['3']"; 
       $Fam_Name_First="$rC['4']";
       $Fam_Name_Middle="$rC['5']";
       $Fam_Name_Last="$rC['6']";
       $Fam_Name_Suffix ="$rC['7']";
       $Fam_Gender="$rC['8']";
       $Fam_DOB="$rC['9']";
       $Fam_Disabled="$rC['10']";
       $Fam_Addy1 ="$rC['11']";
       $Fam_Addy2 ="$rC['12']";
       $Fam_City="$rC['13']";
       $Fam_State_Prov="$rC['14']";
       $Fam_Zip="$rC['15']";
       $Fam_Country="$rC['16']";
       $Fam_Home_Phone ="$rC['17']";
       $Fam_Work_Phone  ="$rC['18']";
       $Fam_Work_Phone_Ext="$rC['19']";
       $Fam_Cell_Phone ="$rC['20']";
       $Fam_Occupation ="$rC['21']";
       $Fam_Employer="$rC['22']";
       $Fam_Primary_Res ="$rC['23']";
       $Fam_Custody="$rC['24']";
       $Fam_Guard_Excuse ="$rC['25']";
       $Fam_ChiDep_Ans ="$rC['26']";
       $Fam_ReaVeh_Ans="$rC['27']";
       $InstructDone ="$rC['28']";
       $SetDone ="$rC['29']";
       $AppDone ="$rC['30']";
       $HouDone ="$rC['31']";
       $DepDone ="$rC['32']";
       $AssDone="$rC['33']";
       $ReaDone ="$rC['34']";
       $IncDone ="$rC['35']";
       $ExpDone ="$rC['36']";
       $QueDone="$rC['37']";
       $ApplicationDone ="$rC['38']";
       $StatusDone ="$rC['39']";
       $Fam_Init="$rC['40']";
       $Fam_Start_Date ="$rC['41']";
       $Fam_End_Date ="$rC['42']";
       $Fam_Agree ="$rC['43']";
       $username ="$rC['44']";
       $password ="$rC['45']";
       $email ="$rC['46']";
       $Fam_Letter="$rC['47']";
       $Exp_Educational ="$rC['48']";
       $Fam_Tax ="$rC['49']";
       $Scan_FileNames="$rC['50']";
       $Scan_Descriptions ="$rC['51']";
       $Fam_Complete ="$rC['52']";
       $referral="$rC[''53]";
       $Fam_Holds="$rC['54']";
       $appealed="$rC['55']";
       $Appeal_Letter="$rC['56']";
       $Appeal_Viewed="$rC['57']";
       $taxDocuments="$rC['58']";
//insert into family with new applicationid
$queryC2="INSERT INTO family (familyID, applicationID, Fam_Type, Fam_Title, Fam_SSNI20, Fam_SSNI20_Select, Fam_Name_First, Fam_Name_Middle, Fam_Name_Last, Fam_Name_Suffix, Fam_Gender, Fam_DOB, Fam_Disabled, Fam_Addy1, Fam_Addy2, Fam_City, Fam_State_Prov, Fam_Zip, Fam_Country, Fam_Home_Phone, Fam_Work_Phone, Fam_Work_Phone_Ext, Fam_Cell_Phone, Fam_Occupation, Fam_Employer, Fam_Primary_Res, Fam_Custody, Fam_Guard_Excuse, Fam_ChiDep_Ans, Fam_ReaVeh_Ans, InstructDone, SetDone, AppDone, HouDone, DepDone, AssDone, ReaDone, IncDone, ExpDone, QueDone, ApplicationDone, StatusDone,  Fam_Init, Fam_Start_Date, Fam_End_Date, Fam_Agree, username, password, email, Fam_Letter, Exp_Educational, Fam_Tax, Scan_FileNames, Scan_Descriptions, Fam_Complete, referral, Fam_Holds, appealed, Appeal_Letter, Appeal_Viewed, taxDocuments   )
    VALUES
    (
     '$newfam1id,'        
     '$newappid,'
     '$Fam_Type,' 
     '$Fam_Title,'
     '$Fam_SSNI20,'
     '$Fam_SSNI20_Select,'
     '$Fam_Name_First,'
     '$Fam_Name_Middle, '
     '$Fam_Name_Last,'
     '$Fam_Name_Suffix,'
     '$Fam_Gender,' 
     '$Fam_DOB,'
     '$Fam_Disabled,'
     '$am_Addy1,' 
     '$Fam_Addy2,' 
     '$Fam_City,'
     '$Fam_State_Prov,'
     '$Fam_Zip,'
     '$Fam_Country,'
     '$Fam_Home_Phone,'
     '$Fam_Work_Phone, '
     '$Fam_Work_Phone_Ext,'
     '$Fam_Cell_Phone,'
     '$Fam_Occupation,'
     '$Fam_Employer,'
     '$Primary_Res,'
     '$Fam_Custody,' 
     '$Fam_Guard_Excuse,'
     '$Fam_ChiDep_Ans,'
     '$Fam_ReaVeh_Ans,' 
     '$InstructDone,' 
     '$SetDone,'
     '$AppDone,'
     '$HouDone,'
     '$DepDone,'
     '$AssDone,'
     '$ReaDone,'
     '$IncDone,'
     '$ExpDone,'
     '$QueDone,'
     '$ApplicationDone,'
     '$StatusDone,' 
     '$Fam_Init,' 
     '$Fam_Start_Date,'
     '$Fam_End_Date,'
     '$Fam_Agree,' 
     '$username,'
     '$password,'
     '$email,'
     '$Fam_Letter,'
     '$Exp_Educational,'
     '$Fam_Tax,'
     '$Scan_FileNames,' 
     '$Scan_Descriptions,'
     '$Fam_Complete,' 
     '$referral,' 
     '$Fam_Holds,' 
     '$appealed,' 
     '$Appeal_Letter,' 
     '$Appeal_Viewed,' 
     '$taxDocuments'   
    )"; echo $queryC2;

     }



 ?>

4 个答案:

答案 0 :(得分:2)

您不需要循环所有行 用这个:

Insert into new_table select * from old_table where {your criteria here}

答案 1 :(得分:2)

mysql有INSERT into table (fields,list) SELECT fields,list FROM another_table语法

答案 2 :(得分:0)

你可以这样做:

INSERT INTO family ( appliction_id, all_the_other_fields )
SELECT 'your_new_app_id', all_the_other_fields  
FROM familY
WHERE familyID='$famid' AND applicationID='$appid'

让我们希望您的$ famid和$ appid变量被转义,是吗?

答案 3 :(得分:0)

您可以使用mysql_fetch_assoc()而不是数组来减少SELECT端的PHP代码量,尤其是因为您只需将返回的数组块重新分配给各个变量:

$query = "SELECT Fam_Type, Fam_Title, Fam_SSNI20, Fam_SSNI20_Select etc... etc...";
$resultC = mysql_db_query($aidDB, $queryC, $connection); echo $queryC;
while($rC = mysql_fetch_assoc($resultC)) {
   ...
}

此时,您最终得到了相关数组,您可以将其作为单个值获取:

echo "Fam_Type is " . $rc['Fam_Type'];
echo "Fam_Title is " . $rc['Fam_Title'];

可以为您节省大量的变量赋值。当然,如果查询中的任何字段内部映射到相同的名称(例如,您正在进行连接并从两个不同的表中检索两个不同的“id”字段),那么您需要对它们进行别名对于不同的名称,所以它们在数组中明显地出现,但这是查询的简单模型:

SELECT table1.id AS table1_id, table2.id AS table2_id ...

将留给:

$result['table1_id'] = ...

作为辅助节点,您应该始终检查查询是否成功。你盲目地认为它有效并尝试检索数据。但是如果查询失败,则mysql函数返回FALSE,这将不是有效的结果语句并且会中断您的获取尝试。至少,添加一些基本的错误检查

$resultC = mysql_db_query(...) or die("MySQL error: " . mysql_error());

将吐出错误消息并解释查询失败的原因,而不是稍后失败的“...不是有效的语句句柄”。同样,从PHP 5.3开始,mysql_db_query()已被弃用,并且在将来的PHP版本中将会消失。可能希望切换到常规mysql_query()