ループ内でクエリを実行しているときはいつでも、MySQL JOIN で同じことを実行できます。
SELECT
a.agent_code, c.commission_type, c.assigned_value
FROM
agent_allocation a
JOIN
assigned_commission c
ON
a.agent_class = c.agent_class
ORDER BY
a.agent_code, c.commission_type
これで、コードを次のように縮小できます-
$stmt_assigned_commission = $conn_bd->prepare('SELECT a.agent_code, c.commission_type, c.assigned_value FROM agent_allocation a JOIN assigned_commission c ON a.agent_class = c.agent_class ORDER BY a.agent_code, c.commission_type');
$stmt_assigned_commission->execute();
$result_stmt_assigned_commission = $stmt_assigned_commission->fetchAll();
if (count($result_stmt_assigned_commission)) {
foreach($result_stmt_assigned_commission as $row) {
echo $row['agent_code'];
echo $row['commission_type'];
echo $row['assigned_value'];
}
}
else{
//
}
このsqlfiddleを参照してください - http://sqlfiddle.com/#!2/dcbcd/5
結果で-
編集
ここで、重複が発生している理由を簡単に確認します。毎回、以前の値を置き換えずにwhile ($i < $count_assigned_commission )
新しい値を追加しているため、基本的にこれを行っています-$agent_distribution[]
// 1st loop
$agent_distribution[] = array("agent_code"=>"AGENT_1"); // New
$agent_distribution[] = array("agent_code"=>"AGENT_2"); // New
// 2nd loop
$agent_distribution[] = array("agent_code"=>"AGENT_1"); // From 1st Loop
$agent_distribution[] = array("agent_code"=>"AGENT_2"); // From 1st Loop
$agent_distribution[] = array("agent_code"=>"AGENT_1"); // New
$agent_distribution[] = array("agent_code"=>"AGENT_2"); // New
// 3rd loop
$agent_distribution[] = array("agent_code"=>"AGENT_1"); // From 1st Loop
$agent_distribution[] = array("agent_code"=>"AGENT_2"); // From 1st Loop
$agent_distribution[] = array("agent_code"=>"AGENT_1"); // From 2nd Loop
$agent_distribution[] = array("agent_code"=>"AGENT_2"); // From 2nd Loop
$agent_distribution[] = array("agent_code"=>"AGENT_1"); // New
$agent_distribution[] = array("agent_code"=>"AGENT_2"); // New
$agent_distribution[] = array("agent_code"=>"AGENT_3"); // New
つまり、ファイナルwhile ($j < $count_agent_distribution)
を行うときは、2 回 (1 回目while ($i < $count_assigned_commission )
のループ)、4 回 (2 回目while ($i < $count_assigned_commission )
のループ)、7 回 (3 回目のwhile ($i < $count_assigned_commission )
ループ) を実行します。
だから結局こうなる―
$i = 0;
while ($i < $count_assigned_commission ) { // 3 Times
// first time
$stmt_agent_distribution = 'select agent_code from agent_allocation where agent_class = '.$agent_class[0];
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_1");
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_2");
if ( count($result_stmt_agent_distribution) ) {
foreach($result_stmt_agent_distribution as $row_agent_distribution) {
$agent_distribution[] = $row_agent_distribution['agent_code'];
}
}
$count_agent_distribution = count($agent_distribution); // 2
$j = 0;
while ($j < $count_agent_distribution) {
// first time
$stmt_calculate->bindValue(':agent_code', $agent_distribution[0]);
$stmt_calculate->bindValue(':commission_type', $commission_type[0]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[0]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_1","assigned_value"=>1500);
// second time
$stmt_calculate->bindValue(':agent_code', $agent_distribution[1]);
$stmt_calculate->bindValue(':commission_type', $commission_type[0]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[0]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_1","assigned_value"=>1500);
$j++;
}
// second time
$stmt_agent_distribution = 'select agent_code from agent_allocation where agent_class = '.$agent_class[1];
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_1");
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_2");
if ( count($result_stmt_agent_distribution) ) {
foreach($result_stmt_agent_distribution as $row_agent_distribution) {
$agent_distribution[] = $row_agent_distribution['agent_code'];
}
}
$count_agent_distribution = count($agent_distribution); // 4
$j = 0;
while ($j < $count_agent_distribution) {
// first time -- THIS IS A DUPLICATE AS IT IS FROM THE 1ST while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN
$stmt_calculate->bindValue(':agent_code', $agent_distribution[0]);
$stmt_calculate->bindValue(':commission_type', $commission_type[1]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[1]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_2","assigned_value"=>250);
// second time -- THIS IS A DUPLICATE AS IT IS FROM THE 1ST while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN
$stmt_calculate->bindValue(':agent_code', $agent_distribution[1]);
$stmt_calculate->bindValue(':commission_type', $commission_type[1]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[1]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_2","assigned_value"=>250);
// third time
$stmt_calculate->bindValue(':agent_code', $agent_distribution[2]);
$stmt_calculate->bindValue(':commission_type', $commission_type[1]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[1]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_2","assigned_value"=>250);
// fourth time
$stmt_calculate->bindValue(':agent_code', $agent_distribution[3]);
$stmt_calculate->bindValue(':commission_type', $commission_type[1]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[1]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_2","assigned_value"=>250);
$j++;
}
// third time
$stmt_agent_distribution = 'select agent_code from agent_allocation where agent_class = '.$agent_class[2];
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_1");
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_2");
$result_stmt_agent_distribution[] = array("agent_code"=>"AGENT_3");
if ( count($result_stmt_agent_distribution) ) {
foreach($result_stmt_agent_distribution as $row_agent_distribution) {
$agent_distribution[] = $row_agent_distribution['agent_code'];
}
}
$count_agent_distribution = count($agent_distribution); // 7
$j = 0;
while ($j < $count_agent_distribution) {
// first time -- THIS IS A DUPLICATE AS IT IS FROM THE 1ST while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN
$stmt_calculate->bindValue(':agent_code', $agent_distribution[0]);
$stmt_calculate->bindValue(':commission_type', $commission_type[2]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_3","assigned_value"=>750);
// second time -- THIS IS A DUPLICATE AS IT IS FROM THE 1ST while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN
$stmt_calculate->bindValue(':agent_code', $agent_distribution[1]);
$stmt_calculate->bindValue(':commission_type', $commission_type[2]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_3","assigned_value"=>750);
// third time -- THIS IS A DUPLICATE AS IT IS FROM THE 2ND while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN
$stmt_calculate->bindValue(':agent_code', $agent_distribution[2]);
$stmt_calculate->bindValue(':commission_type', $commission_type[2]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_3","assigned_value"=>750);
// fourth time -- THIS IS A DUPLICATE AS IT IS FROM THE 2ND while ($i < $count_assigned_commission ) AND SHOULD NOT BE DONE AGAIN
$stmt_calculate->bindValue(':agent_code', $agent_distribution[3]);
$stmt_calculate->bindValue(':commission_type', $commission_type[2]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_3","assigned_value"=>750);
// fifth time
$stmt_calculate->bindValue(':agent_code', $agent_distribution[4]);
$stmt_calculate->bindValue(':commission_type', $commission_type[2]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_1","commission_type"=>"energy_type_3","assigned_value"=>750);
// sixth time
$stmt_calculate->bindValue(':agent_code', $agent_distribution[5]);
$stmt_calculate->bindValue(':commission_type', $commission_type[2]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_3","assigned_value"=>750);
// seventh time
$stmt_calculate->bindValue(':agent_code', $agent_distribution[6]);
$stmt_calculate->bindValue(':commission_type', $commission_type[2]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[2]);
$stmt_calculate->execute();
$results[] = array("agent_code"=>"AGENT_2","commission_type"=>"energy_type_3","assigned_value"=>750);
$j++;
}
$i++;
実際の修正
$agent_distribution
したがって、各ループで「新しい」を作成する必要があるため、変更します-
$agent_distribution[] = $row_agent_distribution['agent_code'];
...
$count_agent_distribution = count($agent_distribution);
...
$stmt_calculate->bindValue(':agent_code', $agent_distribution[$j]);
に
$agent_distribution[$i][] = $row_agent_distribution['agent_code'];
...
$count_agent_distribution = count($agent_distribution[$i]);
...
$stmt_calculate->bindValue(':agent_code', $agent_distribution[$i][$j]);
だから今は次のようになります
...
while ($i < $count_assigned_commission ) {
$stmt_agent_distribution = $conn_bd->prepare('select agent_code from agent_allocation
where agent_class = :agent_class');
$stmt_agent_distribution->execute(array('agent_class' => $agent_class[$i]));
$result_stmt_agent_distribution = $stmt_agent_distribution->fetchAll();
if ( count($result_stmt_agent_distribution) ) {
foreach($result_stmt_agent_distribution as $row_agent_distribution) {
$agent_distribution[$i][] = $row_agent_distribution['agent_code'];
}
} else {
$agent_distribution[$i] = 0;
}
$count_agent_distribution = count($agent_distribution[$i]);
$j = 0;
while ($j < $count_agent_distribution) {
$stmt_calculate->bindValue(':agent_code', $agent_distribution[$i][$j]);
$stmt_calculate->bindValue(':commission_type', $commission_type[$i]);
$stmt_calculate->bindValue(':assigned_value', $assigned_value[$i]);
$stmt_calculate->execute();
$j++;
}
$i++;
}
今$agent_distribution
はこのようになり、重複した結果が削除されます
// 1st loop
$agent_distribution[0][] = array("agent_code"=>"AGENT_1");
$agent_distribution[0][] = array("agent_code"=>"AGENT_2");
// 2nd loop - does not have the rows from the 1st loop
$agent_distribution[1][] = array("agent_code"=>"AGENT_1");
$agent_distribution[1][] = array("agent_code"=>"AGENT_2");
// 3rd loop - does not have the rows from the 1st and 2nd loops
$agent_distribution[2][] = array("agent_code"=>"AGENT_1");
$agent_distribution[2][] = array("agent_code"=>"AGENT_2");
$agent_distribution[2][] = array("agent_code"=>"AGENT_3");
ループ内でループを実行すると、この種のエラーが発生しやすくなり、理由がわからずに結果がすぐに拡大するため、JOIN クエリを引き続きお勧めします。