-1

我想在mysql中同时插入和更新两个表。我有下面的表和详细信息

CREATE TABLE Info
(
 id int auto_increment primary key, 
 BBP decimal(19,2) default "0.00" 

);


create table bill(Amount_paid decimal(19,2) default "0.00",
BAP decimal(19,2) default "0.00"
);    


INSERT INTO Info(BBP)
 VALUES
('30');


INSERT INTO bill(bid,Amount_paid,BAP)
SELECT i.id,
   10,
   i.BBP -
(SELECT COALESCE(sum(b.AMOUNT_PAID),0)+10
FROM bill b
WHERE bid = i.id)
 FROM info i
WHERE i.id = 1;

update info set BBP = (select BBP - coalesce(sum(Amount_paid),0) from bill WHERE id=1);

基本上 BBP 预计会在插入 Amount_paid 后更新,但它只会在第一次插入 Amount_paid 时正确更新,而在第二次和其他插入后不正确。每件事都按安排工作,但更新查询没有按预期工作。请我希望有人教我我的更新查询或任何更好的查询哪里出错了。谢谢你的帮助

4

1 回答 1

0

你试过了吗

update info set BBP = BBP - (select coalesce(sum(Amount_paid),0) from bill) WHERE id=1;
于 2013-07-11T18:59:34.427 回答