0

如何获得 Purchase_Order.QTY 和 Purchase_Request.QTY 的总和作为余额?我的问题是 Purchase_Order 表中有多个 po_number 具有相同的计数器。

下面是我的表格,

我需要在purchase_order中获取柜台100001的总数量,这样我才能得到QTY purchase_order和QTY purchase_request之间的差异

表采购_订单

counter | qty |

100001  | 10  |
100001  | 10  |
100001  | 10  |
100004  | 30  |

Table Purchase_Request

counter | total_qty |

100001  |     50    |
100002  |     100   |
100003  |     50    |
100004  |     70    |

这是我的示例输出

输出

counter | total_qty | balance |

100001  |     50    |  20     |
100002  |     100   |  100    |  
100003  |     50    |  50     |
100004  |     70    |  40     |

这是我的剧本,

<?php
$mysqli = new mysqli("localhost", "root", "", "test");

    $result = $mysqli->query("

    ");
    echo'<table id="tfhover" cellspacing="0" class="tablesorter" style="text-transform:uppercase;" border="1px">
        <thead>
        <tr>
        <th></th>
    <th>counter</th>
    <th>QTY</th>
    <th>balance</th>
        </tr>
        </thead>';
        echo'<tbody>';
    $i=1;   
while($row = $result->fetch_assoc()){
    echo'<tr>
            <td>'.$i++.'</td>
            <td>'.$row['counter'].'</td>
            <td>'.$row['total_qty'].'</td>
            <td>'.$row['balance'].'</td>
        </tr>';
       }
    echo "</tbody></table>";

?>

帮助 ?

4

4 回答 4

0

您可以在计数器编号上加入表 1 和 2,然后使用 sql 进行计算

mysql_query("SELECT *, O.QTY- R.QTYAS differenceFROM `Purchase_Order AS O LEFT OUTER JOIN Purchase_Request AS R ON O.counter = R.counter");

SELECT 
O.id, 
O.QTY,
COALESCE(O.QTY - (SELECT R.QTY 
                 FROM Purchase_Request AS R
                 WHERE O.counter = R.counter)) AS difference 
FROM Purchase_Order AS O; 
于 2013-11-14T06:34:20.253 回答
0

try this

select a.counter,a.po_number,a.unit_cost,sum(a.qty)-b.qty as ramaining_order_qty
    from table a
    inner join table b on a.counter=b.counter 
    group by a.counter,a.po_number,a.unit_cost,b.qty
于 2013-11-14T06:33:00.283 回答
0
SELECT PR.Counter,pr.qty-po.qty as remainingqty FROM Purchase_Request pr LEFT JOIN (Select counter,sum(qty) from Purchase_Order group by counter)po ON pr.Counter=po.Counter
于 2013-11-14T06:28:25.630 回答
0

这是您的一个选择。

  1. 获取表 Purchase_OrderWHERE计数器中的所有记录 = to Purchase_Request.counter
  2. 循环遍历结果QTY得到一个sum
  3. 将总和QTY与主表中的总和进行比较,以确定您的订单是否可以成交。

这是一种选择。另一个建议是清理你的数据库。您也许还可以进行 SQL 连接。

于 2013-11-14T06:29:15.040 回答