我有一个查询,我从DB2数据库中选择记录并将值存储在数组中:
$orderIdsStr = "'" . implode("', '", $order_ids) . "'";
//SELECT FROM DB2 WITH THE ORDER NUMBERS FIRST
$query = "SELECT invnoc as INVOICE,
cstnoc AS DEALER,
framec AS FRAME,
covr1c AS COVER,
colr1c AS COLOR ,
extd2d AS SHIPDATE,
orqtyc AS QUANTITY
FROM GPORPCFL
WHERE invnoc IN ({$orderIdsStr})
GROUP BY invnoc,cstnoc, slsnoc, orqtyc, framec, covr1c,colr1c, extd2d
ORDER BY invnoc asc
LIMIT 35";
$Db2ShipRslt = odbc_exec($DB2Conn, $query);
if ( $Db2ShipRslt === false ) {
exit (odbc_errormsg($DB2Conn));
}
//Process the results
$Db2ShipArr = array();
while($db2ShipRow = odbc_fetch_array($Db2ShipRslt)){
{
//Output the record
print_r($db2ShipRow); /*This prints the correct Rows*/
//Append record to results array
$Db2ShipArr[] = $db2ShipRow;
}
这样做有效,可以正确记录并存储正确的记录。
我还有一个正常工作的插入(它插入了确切数量的记录和适当的值)。我想知道如何在插入物中应用某种条件,我从来没有这样做过。
我的选择将名为orqtyc
的值作为别名QUANTITY
抓取,然后将这些值插入名为placements_new
的列中的mysql locations
表中。这是合适的,但我也希望能够让每个记录多次插入等于该值的记录。
基本上if row is < :QUANTITY , keep inserting until the record is inserted n times, n being :QUANTITY value
。因此,如果:QUANTITY
= 3,则应插入相同的记录3次。从技术上讲,会有重复,但这样每行都有自己的日期集。
同样,select / insert工作和数组都返回了我需要的东西。我需要的一些帮助是如何使插入写入行数=数量值。
这是插入发生的另一部分:
foreach($Db2ShipArr as $Db2Ship){
/*THIS IS THE PROBLEM AREA*/
if($rows < :QUANTITY, INSERT n = :QUANTITY){
//INSERT # of records equal to QUANTITY
$stmt = $PDO->prepare("
INSERT IGNORE INTO placements_new (sku_id, group_id, dealer_id, start_date, expire_date, locations, order_num)
SELECT
id,
sku_group_id,
:DEALER,
DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 7 DAY) as start_date,
DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY) as expire_date,
:QUANTITY,
:INVOICE
FROM skus s
WHERE s.frame=:FRAME AND s.cover1=:COVER AND s.color1=:COLOR
");
$PDO->beginTransaction();
$i = 0;
while($db2row = odbc_fetch_array($Db2ShipRslt)) {
if(++$i % 1000 == 0) {
$PDO->commit();
$PDO->beginTransaction();
}
$stmt->execute($db2row);
}
$PDO->commit();
}
}
}
}
这是我制作的小提琴。所以在这个小提琴中,我为GPORPCFL插入的记录将通过php logic / looop插入placements_new 3次,因为3是orqtyc
的值: