简体   繁体   English

如何获得不同表中两个字段的总和?

[英]How to get the sum of two fields in different table?

How to get the sum of Purchase_Order.QTY and Purchase_Request.QTY as balance? 如何获取Purchase_Order.QTY和Purchase_Request.QTY的总和作为余额? My problem is there are multiple po_number in Purchase_Order table with has a same counter. 我的问题是Purchase_Order表中有多个具有相同计数器的po_number。

Below is my Tables, 下面是我的桌子,

I need to get the total QTY of counter 100001 in purchase_order, so I can get the difference between QTY purchase_order and QTY purchase_request 我需要在purchase_order中获得计数器100001的总QTY,因此我可以得到QTY Purchase_order和QTY Purchase_request之间的差额

Table Purchase_Order 表Purchase_Order

counter | qty |

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

Table Purchase_Request 表Purchase_Request

counter | total_qty |

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

And this my example OUTPUT 这是我的示例输出

OUTPUT 输出值

counter | total_qty | balance |

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

And this is my script, 这是我的剧本,

<?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>";

?>

Help ? 救命 ?

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

Here is an option for you. 这是您的一个选择。

  1. Get all records in Table Purchase_Order WHERE counter is = to Purchase_Request.counter 获取表Purchase_Order的所有记录,其中WHERE计数器= = Purchase_Request.counter
  2. Loop through the results of QTY to get a sum 循环浏览QTY的结果以获得sum
  3. Compare the sum to QTY from the main table to determine if your order can be filled. 将总和与主表中的“ QTY进行比较,以确定是否可以执行您的订单。

This is one option. 这是一种选择。 Another suggestion would be to clean up your database. 另一个建议是清理数据库。 You might also be able to do SQL joins. 您也许还可以执行SQL连接。

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

You could join table 1 and 2 on counter number, then use sql to do the calculating 您可以将表1和2连接到计数器编号上,然后使用sql进行计算

mysql_query("SELECT *, O.QTY - R.QTY AS difference FROM `Purchase_Order AS O LEFT OUTER JOIN Purchase_Request AS R ON O.counter = R.counter"); mysql_query(“ SELECT *, O.QTY - R.QTY AS difference `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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM