【问题标题】:How to update a column when the same column in another table is changed ?(postgresql)更改另一个表中的同一列时如何更新列?(postgresql)
【发布时间】:2013-07-10 12:19:26
【问题描述】:

我需要一个函数或触发器来解决这个问题??

customer_details :::

custid    name    creditid
----------------------------
   2        a        1 
   3        b        2
   4        c        3

余额金额 :::

creditid   credit_type    balance
-----------------------------------
    1         rent        1000
    1         transport   2000
    1         food        1000
    1         fruits      1500
    2         rent        1500
    2         transport   1020
    2         food        1200
    2         fruits      1000
    3         transport   1600
    3         rent        2000
    3         food        1540
    3         fruits      1560

Pay_the_loan :::

creditid   credit_type       Pay      status
---------------------------------------------
    1         rent           500       null
    2         fruits         600       null
    3         transport      400       null
    1         fruits         500       null

一旦我将pay_the_loan table 中的status 列更新为ok 以获取特定的creditid,即,

(更新 pay_the_loan set status='ok' where creditid=2)

then 它应该是 deduct balance_amount 表中 balance 列中的金额,并且应该更新它,即 balance_amount 表中的 (1000-600=400 where balance_amount.credit_type=fruits 和creditid=2 来自余额金额表)

可以给我发FunctionTrigger 来解决这个问题吗?

【问题讨论】:

  • 1) 定义条件:什么时候应该更新? 2) 定义实际的更新语句:应该改变什么,如何改变? 3)2 转换为函数,将1 转换为触发器。 4) 利润! 0) 您需要在表格中添加一些 PG/FK。
  • 好的,我会举一个这样的例子,这样可以更好地理解这一点!问题@wildplasser

标签: sql postgresql


【解决方案1】:

您最好稍微重组一下,创建一个包含原始贷款的loan_amount 表,然后创建一个显示当前余额并扣除所有付款的balance_amount 视图。这样,例如,更正付款金额不会使您的系统显示错误的余额。

为您计算当前余额的视图可能类似于:

CREATE VIEW balance_amount AS
    SELECT la.creditid, la.credit_type, amount 
              - COALESCE(SUM(pay),0) balance
    FROM loan_amount la
    LEFT JOIN pay_the_loan pl
      ON la.creditid = pl.creditid 
     AND la.credit_type = pl.credit_type
     AND pl.status = 'OK'
    GROUP BY la.creditid, la.credit_type, la.amount;

An SQLfiddle with the whole setup.

【讨论】:

【解决方案2】:

您可以在同一个查询中更新两个表:

update
    pay_the_loan
set
    pay_the_loan.status='ok',
    balance_amount.balance=balance_amount.balance-pay_the_loan.Pay
from balance_amount
where
    pay_the_loan.creditid=balance_amount.creditid
    and
    pay_the_loan.creditid=2
    and
    balance_amount.credit_type='fruits';

更新。我已阅读 the documentation 的 postgresql update 语句。显然,我错了,不可能在一个查询中更新两个表。但是,我仍然认为您在这里不需要触发器。只需一个接一个地使用两个更新查询:

update
    pay_the_loan
set
    status='ok'
where
    pay_the_loan.creditid=2;

update
    balance_amount
set
    amount=balance_amount.amount-pay_the_loan.Pay
from pay_the_loan
where
    pay_the_loan.creditid=balance_amount.creditid
    and
    pay_the_loan.creditid=2
    and
    balance_amount.credit_type='fruits';

【讨论】:

  • 这是 SQL FIDDLE sqlfiddle.com/#!12/3e6a7/18 请解决这个查询...@mikhail makarov
  • 你试过我的方法了吗?您不需要函数或触发器。您可以在将状态设置为“确定”的同一查询中更新余额。你明白我的建议了吗?
  • 我已经阅读了 postgresql 更新语句的文档。显然,我错了,不可能在一个查询中更新两个表。但是,我仍然认为您在这里不需要触发器。只需一个接一个地使用两个更新查询。我编辑了我的答案。
猜你喜欢
  • 2012-11-08
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 2019-07-25
  • 2021-01-08
  • 1970-01-01
  • 2019-10-13
  • 1970-01-01
相关资源
最近更新 更多