嗨,我有2个mysql表,它们都需要更新,
companies
unique_code
公司拥有100万条记录,我需要为每条记录分配一个唯一的代码,我的问题是我的PHP脚本抓住了所有公司,并且在foreach循环中,从表unique_code中获取了唯一代码并进行更新,然后还将unique_code表更新为flag该代码已被使用.
PHP代码挂起了很长时间,并达到了最大执行限制.我真的很固执,需要这些公司拥有唯一的代码,有人可以考虑其他方法吗?
压缩代码示例.
foreach ($aCompanies as $companies){
$query="SELECT * FROM unique_code WHERE used = 0"
foreach(unique_code as code){
// UPdate companies table
$query = "UPDATE companies SET id = $code";
// Flag code used
$query = "UPDATE unique_codes WHERE code = $code";
}
}
为您的时间加油.
完整的代码:
$query1 = "SELECT code FROM unique_codes WHERE used = 0\n";
$aUniqueCode = $oDbh->getAll($query1);
$query2 = "SELECT id FROM companies";
$aCompanies = $oDbh->getAll($query2);
foreach ($aCompanies as $companies){
$query = "SELECT code FROM unique_codes WHERE used = '0' LIMIT 1";
$oCode = $oDbh->getRow($query);
$query3.= "UPDATE companies SET code = $oCode->code WHERE id = $companies->id\n";
$query4 = "UPDATE unique_codes SET used = '1' WHERE code = $oCode->code\n";
$oDbh->query($query4);
}
print print_r($query3).';';
exit;
最佳答案