我的SELECT查询可以正确地获取所有内容,但是我的插入失败,因为我的参数值包含太多的插入内容(frame2,cover2,color2等)
第二个选择需要额外的参数,但插入时不需要它们。
有没有一种方法可以执行我的插入操作,并说“对不带:frame2,:cover2,:color2,:frame3,:cover3,:color3的$ salesValues执行”
$selectPlacements = "
SELECT DISTINCT
sku_id, s.frame as frame, sg.group_code as group_code, sg.name as group_name, s.cover1 as cover, s.color1 as color, dealer_id, sales_rep as repNum, count(*) as placements
FROM place p
inner join skus s
on p.sku_id = s.id
inner join sku_groups sg
on sg.id = s.sku_group_id
where p.expire_date > curdate()
group by sku_id, s.frame, dealer_id
;
";
try {
$placementCheck = $MysqlConn->prepare($selectPlacements);
$detailRslt = $placementCheck->execute();
while ($placementRow = $placementCheck->fetch(PDO::FETCH_ASSOC)) {
print_r($placementRow); //This prints the array I need
$salesValues = [
":dealer_id" => $placementRow["dealer_id"],
":frame" => $placementRow["frame"],
":cover" => $placementRow["cover"],
":color" => $placementRow["color"],
":frame2" => $placementRow["frame"],
":cover2" => $placementRow["cover"],
":color2" => $placementRow["color"],
":frame3" => $placementRow["frame"],
":cover3" => $placementRow["cover"],
":color3" => $placementRow["color"],
":group_code" => $placementRow["group_code"],
":group_name" => $placementRow["group_name"],
":placements" => $placementRow["placements"],
":repNum" => $placementRow["repNum"],
];
$insertSales = "
MERGE INTO Tmetrics AS T
USING (VALUES (
CAST(:dealer_id as CHAR(10)),
CAST(:frame as CHAR(10)),
CAST(:cover as CHAR(10)),
CAST(:color as CHAR(10)),
CAST(:group_code as CHAR(10)),
CAST(:group_name as CHAR(25)),
CAST(:sales180Cust as CHAR(10)),
CAST(:velocityCust as CHAR(10)),
CAST(:repnum as CHAR(10)),
CAST(:sales180Rep as CHAR(10)),
CAST(:velocityRep as CHAR(10)),
CAST(:sales180Comp as CHAR(10)),
CAST(:velocityComp as CHAR(10)),
CAST(:placements as CHAR(10))
)
)
AS S(dealer, frame, cover, color, groupnum, groupname, last180days_sales,velocity,repnumber,last180_rep,velocityrep,last180company,velocitycompany,placements)
ON S.dealer = T.dealer and S.frame = T.frame and S.cover = T.cover and S.color = T.color
WHEN MATCHED
THEN UPDATE SET last180days_sales = S.last180days_sales, velocity = S.velocity, repnumber = S.repnumber, last180_rep = S.last180_rep, velocityrep = S.velocityrep, last180company = S.last180company, velocitycompany = S.velocitycompany
WHEN NOT MATCHED
THEN INSERT VALUES(S.dealer, s.frame, s.cover, s.color, s.groupnum, s.groupname, s.last180days_sales,s.velocity,s.repnumber,s.last180_rep,s.velocityrep,s.last180company,s.velocitycompany,s.placements)
";
try{
$velocityExecution = $velocityInsertPrep->execute($salesValues);
}catch(PDOException $ex){
echo "INSERT_FAILED: " .$ex->getMessage();
}
答案 0 :(得分:0)
没有一种方法可以将值传递给PDOStatement::execute()
并告诉PDO不要使用它们,但是您可以使用纯PHP {{ 1}}:
execute()