我正在尝试从两个不同的表中获取零件编号,并将它们放在同一阵列中,然后重新排列它们。
奇怪的是它给出了两次零件编号,但是当我在phpmyadmin中运行相同的查询时,它给出了每个零件编号一次。 我花了一整天,但无法纠正这一点。
//first query
$finalData = array(); $sql="SELECT
jobc_parts_p.part_no,
SUM(jobc_parts_p.issued_qty) AS sale_qty FROM
`jobc_parts_p` WHERE DATE_FORMAT(jobc_parts_p.date_time,'%Y-%m-%d') BETWEEN '".$f."' AND '".$t."'
GROUP BY jobc_parts_p.part_no";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
$finalData[$row['part_no']][] = $row;
}
//第二个查询
$sql2="SELECT
jobc_consumble_p.part_no,
SUM(jobc_consumble_p.issued_qty) AS csale_qty
FROM
`jobc_consumble_p`
WHERE DATE_FORMAT(jobc_consumble_p.date_time,'%Y-%m-%d') BETWEEN '".$f."' AND '".$t."'
GROUP BY
jobc_consumble_p.part_no";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
$finalData[$row['part_no']][] = $row;
}
///重新整理数据...
$rearrangedFinalData = array();
foreach($finalData AS $first) {
foreach($first AS $data) {
$temp = array();
$temp['part_no'] = $data['part_no'];
$temp['sale_qty'] = isset($data['sale_qty']) ? $data['sale_qty'] : $data['csale_qty'];
$rearrangedFinalData[] = $temp;
}
}
//输出结果
foreach($rearrangedFinalData AS $row) {
$sr++;
echo "
$sr",$row["part_no"],"",$row["sale_qty"],"";}
结果
1 10R46 2
2 10R46 2
3 10R91 1
4 10R91 1
5 10M95 3
6 10M95 3
我想要的是:
1 10R46 2
2 10R91 1
3 10M95 3
首先查询print_r($ finalData);
Array (
[10R46 ] => Array ( [0] => Array ( [part_no] =>
10R46 [sale_qty] => 1 ) )
[10R91 ] => Array ( [0] => Array ( [part_no] =>
10R91 [sale_qty] => 3 ) ))