从 2 个表中添加具有相同名称的数量

Adding Quantities that have same names from 2 tables

我有一个 table,它的部件号包含 15 个字符,在这个字符串中,一个数字包含 4 个不同的功能。我正在查询 table,然后剥离不同的数字字符串,然后在其他 table 中使用。我遇到的问题是当有多个具有相同值的子字符串时。然后我通过我的物料清单 table 运行它以获得制造整个零件所需的数量。但是当我想把它变成 return 所有重复项的总和时,它是 returning 重复项。

这就是我正在做的事情:

$result = mysql_query("SELECT * FROM $table WHERE `active` = '1'");
    while ($r = mysql_fetch_assoc($result)) {
        $bucket_back_num = substr($r['part_num'],0,7);
        $bucket_bottom_num = substr($r['part_num'],0,7);
        $hooks_part_num = substr($r['part_num'],7,11-7);
        $grapple_part_num = substr($r['part_num'],11,15-11);
        $bucket_back_num .= $grapple_part_num;

        $result2 = mysql_query("SELECT *,sum(qty) as qty from bucket_part_bom WHERE `parent_part_num` = '$bucket_back_num' GROUP BY parent_part_num");
        while ($r2 = mysql_fetch_assoc($result2)) {
            echo '
                <tr>
                    <td style="width: 15%;">'.$r2['part_num'].'</td>
                    <td style="width: 25%; text-align: left;">'.$r2['desc'].'</td>
                    <td style="width: 15%;">'.$r2['qty'].'</td>
                    <td style="width: 15%;">Unpainted</td>
                    <td style="width: 10%;">Diff</td>
                    <td style="width: 10%;">Build</td>
                    <td style="width: 10%;">N/A</td>
                </tr>
            ';
        }
    }

正在发生什么return编辑:

LM102BRBK   Bucket Back 102" Light Material Bare    1
LM1023XBK   Bucekt Back 102" Light Material, GF3X   1
LM1023XBK   Bucekt Back 102" Light Material, GF3X   1

将两个查询合并为一个查询 JOIN

SELECT t.*, b.*, SUM(b.qty) AS qty
FROM $table AS t
JOIN bucket_part_bom AS b ON b.parent_part_num = SUBSTR(t.part_num, 1, 7)
GROUP BY b.parent_part_num