且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据不同表中的值更新表中的值

更新时间:2023-11-26 21:53:16

需要在触发器中进行,这里更新只访问刚刚添加的订单,(新插入的订单),而不是所有订单之前添加的.

You need to do this in a trigger, where the update is only accessing the orders that have just been added, (newly inserted orders), and not all orders previously added.

Update credits c set 
  credits = credits + 
    (Select sum(credits) from orders
     where student_id = c.student_Id)

或者,您需要标记orders表中的每一行是否已经被计入credits表.

Or, you need to flag each row in the orders table as to whether it has already been counted into the credits table.

 Update credits c set 
  credits = credits + 
    (Select sum(credits) from orders
     where student_id = c.student_Id 
        and added = 0)

然后设置标志表示订单已添加.

and then set the flag to indicate that the order has been added.

 Update orders set added = 1 
 where added = 0