我有一个我一直在运行的php脚本,它似乎一直在工作,但现在我想知道我的一些逻辑是否可能是错误的。
我从db表中选择日期范围内的记录,并将其放入名为$validCount的数组中。
如果数组不是空的,这意味着我有有效的记录要用我的值更新,如果它是空的,我就插入。插入的诀窍是,如果STORES小于Quantity,则它只插入与STORES一样多的内容,否则它将插入与Quantity一样多的内容。
因此,如果使用had插入记录
Stores: 14 Quantity:12那么它只会插入12条记录,但如果有
Stores:1 Quantity:20它只会插入一条记录。
简而言之,对于每个客户,我应该只拥有与他们拥有的商店一样多的有效记录(在有效日期范围内)。如果他们有20个商店,我可以有1到2个记录,但不应该有30个。
更新看起来很好,但我不确定它是否更新了正确的记录,尽管在某些情况下,它似乎只是插入了太多记录,而没有考虑过去更新的记录。
这里的逻辑有没有完全不正确的地方?
if(!empty($validCount)){
for($i=0; $i<$row2['QUANTITY']; $i++){
try{
$updateRslt = $update->execute($updateParams);
}catch(PDOException $ex){
$out[] = $failedUpdate;
}
}
}else{
if($row2["QUANTITY"] >= $row2["STORES"]){
for($i=0; $i<$row2["STORES"]; $i++){
try{
$insertRslt = $insert->execute($insertParams);
}catch(PDOException $ex){
$out[] = $failedInsertStore;
}
}
}elseif($row2["QUANTITY"] < $row2["STORES"]){
for($i=0; $i<$row2["QUANTITY"]; $i++){
try{
$insertRslt = $insert->execute($insertParams);
}catch(PDOException $ex){
$out[] = $failedInsertQuantity;
}
}
}
}更新:
假设客户123购买了4个产品A,他们有10个地点
customerNumber | product | category | startDate | expireDate | stores
----------------------------------------------------------------------------------
123 1 A 2018-08-01 2019-03-01 10
123 1 A 2018-08-01 2019-03-01 10
123 1 A 2018-08-01 2019-03-01 10
123 1 A 2018-08-01 2019-03-01 10因为他们购买的数量少于他们的商店数量,所以我插入了4条记录。现在,如果我的$validCheck查询选择了所有4条记录(因为它们在有效的日期范围内),并且我的循环发现数组不是空的,那么它就知道需要更新这些记录或插入这些记录。假设他们这次买了15个。然后,我需要插入6条记录,然后更新其他9条记录的到期日期。
customerNumber | product | category | startDate | expireDate | stores
----------------------------------------------------------------------------------
123 1 A 2018-08-01 2019-03-11 10
123 1 A 2018-08-01 2019-03-11 10
123 1 A 2018-08-01 2019-03-11 10
123 1 A 2018-08-01 2019-03-11 10
123 1 A 2018-08-01 2019-03-11 10
123 1 A 2018-08-01 2019-03-11 10
123 1 A 2018-08-01 2019-03-11 10
123 1 A 2018-08-01 2019-03-11 10
123 1 A 2018-08-01 2019-03-11 10
123 1 A 2018-08-01 2019-03-11 10在有效日期范围内,该客户和产品最多只能有10条(门店数量)记录。一旦该客户/产品的行数达到商店的等价物,它现在就需要遍历并更新等于数量的值
更新
当前正在运行此命令:
$total = $row2['QUANTITY'] + $validCheck;
if ($total < $row2['STORES']) {
$insert_count = $row2['QUANTITY'];
$update_count = 0;
} else {
$insert_count = $row2['STORES'] - $validCheck; // insert enough to fill all stores
$update_count = ($total - $insert_count); // update remainder
}
foreach ($i = 0; $i < $insert_count; $i++) {
try {
$insertRslt = $insert->execute($insertParams);
} catch(PDOException $ex){
$out[] = $failedInsertStore;
}
}
if ($update_count > 0) {
try {
$updateParams[':UPDATELIMIT'] = $update_count;
$updateRslt = $update->execute($updateParams);
} catch(PDOException $ex){
$out[] = $failedInsertStore;
}
}发布于 2019-01-08 06:02:07
我认为逻辑应该是这样的:
$total = $row2['QUANTITY'] + $validCheck;
if ($total < $row2['STORES']) {
$insert_count = $row2['QUANTITY'];
$update_count = 0;
} else {
$insert_count = $row2['STORES'] - $validCheck; // insert enough to fill all stores
$update_count = ($total - $insert_count); // update remainder
}然后在插入新行的for循环中使用$insert_count作为重复计数。UPDATE查询应该包含LIMIT :limit,然后就可以将$update_count绑定到:limit占位符。
for ($i = 0; $i < $insert_count; $i++) {
try {
$insertRslt = $insert->execute($insertParams);
} catch(PDOException $ex){
$out[] = $failedInsertStore;
}
}
if ($update_count > 0) {
try {
$updateParams[':limit'] = $update_count;
$updateRslt = $update->execute($updateParams);
} catch(PDOException $ex){
$out[] = $failedInsertStore;
}
}https://stackoverflow.com/questions/54081530
复制相似问题