我试图在php中一起编写脚本,使用sql从DB2获取查询,然后使用该结果集将其等同于另一个连接中的mysql表中的数据,并根据该结果插入结果到另一个。
基本上,我的db2查询返回确定My SQL中表的ID所需的几个字段
下面我有一个例子:
我从这里选择: DB2查询结果
Dealer | Style | Frame | Cover | Color | Placements | shipdate
---------------------------------------------------------------
123 1 1234 12 1 2 20180219
123 2 1235 12 1 2 20180219
123 3 1236 12 1 2 20180219
在某种意义上,我需要将以前的数据加入到以下数据中 SKU表(db2.style = sku.groupID,db2.frame = sku.frame,db2.cover = sku.cover和db2.color = sku.color)以获取正确的ID
ID | Frame | GroupID | cover | color
------------------------------------
15 1234 1 12 1
16 1235 2 12 1
17 1236 3 12 1
接下来,我需要插入先前确定的ID,以及原始DB2查询中的一些数据(将样式插入groupID,经销商转换为dealerID,发送到startdate,放置到展示位置)
INSERT将导致:(skuplacement table)
sku_id | groupID | dealerID | startDate | expirationDate | placements
------------------------------------------------------------------------------
15 1 123 20180226 (shipdate + 127 days) 2
16 2 123 20180226 (shipdate + 127 days) 2
17 3 123 20180226 (shipdate + 127 days) 2
我希望这是有道理的。
我现在已经包含了我的完整脚本,但缺少MYSQL的我的INSERT / SELECT / JOIN。我现在不确定如何解决这个问题,但我有两个联系。这只是创建正确查询的问题。
我很乐意回答任何问题以消除困惑。
<?php
//Establilsh MySql Connection
$mysqlConn = new mysqli($mysqlServer, $mysqlUser, $mysqlPass);
//Check MySQL connection
if($mysqlConn->connect_error){
die("Connection Failed: " .$mysqlConn->connect_error);
}
echo "Connected Succssfully to Mysql";
//Establish DB2 connection
$DB2Conn = odbc_connect();
//Check DB2 Connection
if(!$DB2Conn){
die("Could not connect");
}else{
echo"Connected to DB2";
}
$plcQueryDB2 = "
select
xcstno as dealer,
p.style as Style,
trim(p.framfmt) as Frame,
p.cover1 as Cover,
p.color1 as Color,
sum(skunoc) as placements,
p.extd1d as shipdate
from pieces p
where left(shipdate, 4) >= '2016'
group by xcstno, xslsno, sup, f.style, f.grpnam, f.framfmt, f.cover1, f.color1, f.coldss,a.extd1d
order by left(shipdate, 4) ASC, REP
";
$prep = odbc_prepare($DB2Conn, $plcQueryDB2);
$exec = odbc_execute($prep);
$result = odbc_exec($DB2Conn, $plcQueryDB2);
if(!$prep){
die("Could Not Run Query");
}else{
echo "DB2 Query Successful";
}
while(odbc_fetch_row($result)){
for($i=1;$i<=odbc_num_fields($result);$i++){
echo "Result is ".odbc_result($result,$i);
}
}
/*Need to get an INSERT created here*/
$InsertSKUS = "
INSERT INTO skuPlacement
values (?,?,?,?,?,?)
";
/* This is my problem. I need to select from another table called skus and insert into skuPlacement based on equating fields from my previous DB2 query and the skus table*/
////*CLOSE CONNECTIONS*////
if (mysqli_close($mysqlConn)){
echo "MySQL Closed";
}
//$CloseDB2 = odbc_close( $DB2Conn);
if(odbc_close( $DB2Conn)){
echo "DB2 Closed";
}
?>
答案 0 :(得分:2)
考虑将DB2查询结果保存到csv文件。然后使用LOAD DATA INFILE
(快速批量工具)将csv文件导入MySQL并在MySQL内部运行所需的连接追加查询:
DB2 查询CSV
cf = boto3.client('cloudformation')
cf.describe_stack_resources(PhysicalResourceId="i-07bd92638b049ccc4")
MySQL 查询(在PHP或控制台中单独运行)
try {
$DB2Conn = odbc_connect();
$plcQueryDB2 = "...";
$prep = odbc_prepare($DB2Conn, $plcQueryDB2);
$exec = odbc_execute($prep);
$result = odbc_exec($DB2Conn, $plcQueryDB2);
}
catch(Exception $e) {
echo $e->getMessage();
}
// Writing column headers
$columns = array('Dealer', 'Style', 'Frame', 'Cover', 'Color', 'Placements', 'shipdate');
$fs = fopen('DB2_Query.csv', 'w');
fputcsv($fs, $columns);
fclose($fs);
// Writing data rows
while($arr = odbc_fetch_array($result)) {
$fs = fopen('DB2_Query.csv', 'a');
fputcsv($fs, $arr);
fclose($fs);
}
odbc_close($DB2Conn);
$DB2Conn = null;