gpt4 book ai didi

php - 使用 DB2 查询加入 mysql 数据进行插入

转载 作者:行者123 更新时间:2023-11-29 05:56:37 27 4
gpt4 key购买 nike

我试图在 php 中获取一个脚本,该脚本使用 sql 从 DB2 获取查询,然后通过将其等同于另一个连接中的 mysql 表中的数据来使用该结果集,并将基于该结果的结果插入到另一个连接中.

基本上,我的 db2 查询返回几个字段,这些字段需要确定我的 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, and 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 查询的一些数据(将 style 插入 groupID,将 dealer 插入 dealerID,将 shipdate 插入 startdate,将 placements 插入 placements)

INSERT 将导致:(skuplacement 表)

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";
}





?>

最佳答案

考虑将您的 DB2 查询结果保存到 csv 文件。然后使用 LOAD DATA INFILE 将 csv 文件导入 MySQL (快速批量工具)并在 MySQL 中运行所需的连接追加查询:

DB2 查询 CSV

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;

MySQL 查询(在 PHP 或控制台中单独运行)

$mysqlConn->query("CREATE TABLE IF NOT EXISTS db2Temp (
dealer INTEGER,
style INTEGER,
frame INTEGER,
cover INTEGER,
color INTEGER,
placements INTEGER,
shipdate DATE
)");

$mysqlConn->query("DELETE FROM db2Temp");

$mysqlConn->query("LOAD DATA LOCAL INFILE /path/to/DB2Query.csv
INTO TABLE db2temp
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES");

$mysqlConn->query("INSERT INTO skuPlacement (sku_id, groupID, dealerID, startDate,
expirationDate, placements)
SELECT sku.ID, sku.Group_ID, db2.dealer, db2.shipDate,
DATE_ADD(
DATE_FORMAT(CONVERT(db2.shipDate, CHAR(8)), '%Y-%m-%d'),
INTERVAL 127 DAY) as expirationDate,
db2.placements
FROM sku
INNER JOIN db2temp db2
ON db2.style = sku.groupID
AND db2.frame = sku.frame
AND db2.cover = sku.cover
AND db2.color = sku.color
WHERE NOT EXISTS
(SELECT 1 FROM skuPlacement p
WHERE sku.ID = p.sku_ID)");

关于php - 使用 DB2 查询加入 mysql 数据进行插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48873641/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com