合并两个mysql查询从不同的数据库在PHP



我需要合并一对MySQL查询从两个数据库分别位于不同的服务器到一个多维关联数组。下面是我正在处理的一个数据示例。

这个问题似乎与结果$data0数组值缺乏唯一键(理想情况下是相应的'sku'值)作为合并数据的索引有关。

期望的输出将是:

Array (
    [0] => Array (
        [SKU] => 101
        [Description] => Test Item 1
        [On Hand 1] => 3
        [On Hand 2] => 7
    )
    [1] => Array (
        [SKU] => 102
        [Description] => Test Item 2
        [On Hand 1] => 11
        [On Hand 2] => 15
    )
)

可能有更好的方法来处理MySQL查询和所需的数组。如果有,请告诉我。请提供任何反馈,因为我是一个排名初学者,将采取所有的帮助,我可以得到。谢谢!

<?
$query1 = "SELECT sku, description, on_hand as 'On Hand 1' FROM database1"
$queryresult1 = mysql_query($query1, $connection1);
$data1 = array();
while ($resultrow1 = mysql_fetch_assoc($queryresult1)) {
    array_push($data1, $resultrow1);
}
$query2 = "SELECT sku, on_hand as 'On Hand 2' FROM database2"
$queryresult2 = mysql_query($query2, $connection2);
$data2 = array();
while ($resultrow2 = mysql_fetch_assoc($queryresult2)) {
    array_push($data2, $resultrow2);
}
$data0 = array_merge($data1, $data2);
//example code for troubleshooting
$data1 = array('101' => array('SKU' => '101', 'Description' => 'Test Item 1', 'On Hand 1' => 3), '102' => array('SKU' => '102', 'Description' => 'Test Item 2', 'On Hand 1' => 11));
print_r($data1);
echo "<br /><br />";
$data2 = array('101' => array('SKU' => '101', 'On Hand 2' => 7), '102' => array('SKU' => '102', 'On Hand 2' => 15));
print_r($data2);
echo "<br /><br />";
$data0 = array_merge($data1, $data2);
print_r($data0);
?>

通过循环$data1中的每个键,可以使用当前的$key$data2中检索值。这将给你你想要的结果:

$data1 = array('101' => array('SKU' => '101', 'Description' => 'Test Item 1', 'On Hand 1' => 3), '102' => array('SKU' => '102', 'Description' => 'Test Item 2', 'On Hand 1' => 11));
$data2 = array('101' => array('SKU' => '101', 'On Hand 2' => 7), '102' => array('SKU' => '102', 'On Hand 2' => 15));
$data0 = array();
foreach($data1 as $key => $data){
    $data['On Hand 2'] = $data2[$key]['On Hand 2'];
    $data0[] = $data;
}
unset($data1, $data2);
print_r($data0);
<?php
// connection 1
$connection1= mysql_connect("localhost","root","");
//selecting database 1
mysql_select_db("database1",$connection1);
// select query from the database1 and table 1
$query1 = "SELECT sku, description, on_hand as 'On Hand 1' FROM database1.table1";
$queryresult1 = mysql_query($query1, $connection1);
$data1 = array();
while ($resultrow1 = mysql_fetch_assoc($queryresult1)) {
    array_push($data1, $resultrow1);
}
//connection 1
$connection2= mysql_connect("localhost","root","");
//selecting database 2
mysql_select_db("database2",$connection2);
// select query from the database2 and table 2
$query2 = "SELECT sku, on_hand as 'On Hand 2' FROM database2.table2";
$queryresult2 = mysql_query($query2, $connection2);
$data2 = array();
while ($resultrow2 = mysql_fetch_assoc($queryresult2)) {
    array_push($data2, $resultrow2);
}
$data0 = array_merge($data1, $data2);
print_r($data0);
?>

您可能最好使用单个UNION查询:

SELECT sku, description, on_hand as 'On Hand 1' FROM database1
UNION ALL
SELECT sku, null       , on_hand as 'On Hand 2' FROM database2

注意database2查询中的描述字段使用null。UNION成员查询必须具有相同的列数和相同的类型。

那么这就是一个简单的问题:

while($row = mysql_fetch_assoc($result)) { 
    $data[] = $row;
}

您可以使用JOIN语法在查询中连接两个表:https://dev.mysql.com/doc/refman/5.0/en/join.html

那么你的查询就像这样:

SELECT
    table1.sku,
    table1.description,
    table1.on_hand as 'On Hand 1',
    table2.on_hand as 'On Hand 2'
FROM
    database1.table1 JOIN
    database2.table2 ON table1.sku = table2.sku

两个表中的行将相互连接,其中sku字段值彼此对应。如果你想从table1和table2中得到所有的结果,你可以创建一个LEFT JOIN

相关内容

  • 没有找到相关文章

最新更新