0

我有一个名为 salesinvoiceitems 的表,其中包含以下字段

  1. 发票号码
  2. 姓名
  3. 数量
  4. 速度
  5. 账本Id

还有一个字段为的余额表

  1. 发票号码
  2. 账本Id
  3. 数量

在余额表中,我有所有包含发票编号的行。我想更新 salesinvoiceitems 表中的金额。金额需要计算如下:

salesinvoicetable 中一张发票的所有行的 SUM OF (RATE X QTY)。

我试过这个但没有用:

INSERT INTO balancetable (ledgerId,invoiceNumber,date,company,triggerredby)
SELECT buyerId,invoiceNumber,invoiceDate,company,"salesinvoices" as triggerredby
FROM salesinvoices

请阐明一些观点。

4

2 回答 2

1

It's unclear why you are trying to insert when you want to update the balance records. Or did you mean MERGE?

If you do want to update the balance table, you could solve it with a sub-select as follows:

UPDATE BALANCE B
  SET AMOUNT = (SELECT ROUND(SUM(QTY * RATE),2) 
                  FROM SALESINVOICEITEMS S
                 WHERE S.INVOICENUMBER = B.INVOICENUMBER)

The same logic could be used on an insert statement.

If you meant to MERGE the data on the balance table, meaning that you would have to insert or update depending on the row's existence, try checking this link out:

How can I merge two MySql tables?

于 2013-06-01T12:10:01.917 回答
0

如果您不想合并,这可能不是最好的方法,但它应该可以工作:

`//Connect To MySQL
mysql_connect("SERVER","USER","PASSWORD");
mysql_select_db("productphotos_userlogins");

//Info Gathering
$infogatherp1 = mysql_query("SELECT * FROM members WHERE name='$myusername'");  
while($infogatherp2 = mysql_fetch_array($infogatherp1)) {
$invoicenumber = $infogatherp2['invoiceNumber'];
$ledgerid = $infogatherp2['ledgerId'];
$amount = $infogatherp2['amount'];
}`

然后您可以使用变量更新另一个表。
注意:方括号 ( []) 中的位是您要从中获取数据的表中的列名。

希望这会有所帮助,祝你好运!

于 2013-06-01T12:22:49.420 回答