【发布时间】:2018-07-30 03:06:17
【问题描述】:
我正在尝试在 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 查询中的一些数据(将 style 插入 groupID,dealer 插入dealerID,shipdate 插入 startdate,placement 插入placement)
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";
}
?>
【问题讨论】:
-
请尝试从您的代码中删除无休止的空白行。它越紧凑,就越容易理解发生了什么。
-
对,对不起.......这是一个巨大的疏忽,我应该再清理一下
-
没什么大不了的,但保持整洁意味着您正在努力使事情尽可能小而专注。