php - PHP循环查询以执行条件插入或更新
问题描述
我有一个似乎一直在运行的 php 脚本,但现在我想知道我的某些逻辑是否可能关闭。
我从一个日期范围内的数据库表中选择记录,并将其放入一个名为的数组中$validCount
如果该数组不为空,则意味着我有可用的值更新有效记录,如果它为空,我只需插入。插入的技巧是,如果STORES
小于Quantity
则它只插入尽可能多的,STORES
否则它插入尽可能多的Quantity
。
因此,如果插入的记录有
Stores: 14 Quantity:12
然后它只会插入 12 条记录,但如果它有
Stores:1 Quantity:20
它只会插入1条记录。
简而言之,对于每个客户,我应该只拥有与他们拥有的商店一样多的有效记录(在有效日期范围内)。如果他们有 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;
}
}
解决方案
我认为逻辑应该是这样的:
$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
}
然后用作循环中$insert_count
的重复计数以插入新行。查询应包括,然后您可以绑定for
到占位符。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;
}
}
推荐阅读
- apache-kafka - Kafka消费者分区在暂停和恢复时重新处理未提交的消息
- java - 从 UI 中的 JPanel 获取多个值
- ruby-on-rails - ActiveAdmin - 删除删除管理员用户选项
- nginx - 如何在nginx中更改服务器主机名
- amazon-web-services - AWS AppSync GraphQL API 仅从 DynamoDB 返回 20 个项目
- swiftmailer - 为什么带有 gmail 的 symfony4 swiftmailer 不起作用?
- mysql - 将 Wamp 服务器升级到 MySQL 8.0.15
- rx-java - 我可以在 PublishSubject 上使用哪一组 RxJava 运算符,它会延迟除第一项之外的所有内容,然后在延迟期间跳过除最后一项之外的所有内容?
- php - 更改 Woocommerce 模板中的列数
- java - Android房间数据库删除查询不删除任何行