【问题标题】:How to AUTOMATICALLY update a value of a column based on condition on another table's column?如何根据另一个表列的条件自动更新列的值?
【发布时间】:2018-10-30 00:52:16
【问题描述】:

所以基本上,我使用的是 Postgresql,我想做的是: 比如说,我们有 2 个表,即 inventory 和 buyList

create table inventory
(item_id serial primary key,
name text not null,
quantity int not null,
price int not null);

insert into inventory values
(1,'a',44,10000),
(2,'b',12,12000),
(3,'c',11,5000),
(4,'d',6,3000);

create table buyList
(buy_id serial primary key,
item_id not null references inventory(item_id),
quantity int not null);

insert into buyList values
(1,2,4),
(2,2,5),
(3,1,1);

所以我想用相关项目的 buyList.quantity 减去 inventory.quantity 值(当然基于 item_id) 例如,当有人购买了 4 个 item 'a',那么 table inventory 中 item 'a' 数量列的值为 40(自动更新)。

编辑: 非常感谢 krithikaGopalakrisnan 的回答, 所以我使用了 krithikaGopalakrisnan 制作的触发器(并稍作修改)

CREATE OR REPLACE FUNCTION trigger() RETURNS trigger AS $$ 
    BEGIN 

    UPDATE inventory SET quantity =  quantity-NEW.quantity WHERE inventory.item_id = NEW.item_id ;

     RETURN NEW;
    END; 
    $$ LANGUAGE plpgsql;

DO $$
DECLARE 
BEGIN
EXECUTE format('CREATE TRIGGER trigger BEFORE INSERT OR UPDATE ON buylist FOR EACH ROW  WHEN (pg_trigger_depth() = 0) EXECUTE PROCEDURE trigger()');
END;
$$ LANGUAGE plpgsql;

但是现在出现了一个新的问题,当库存表(inventory.quantity)中的商品数量为0,并且在buylist表中有新的购买该商品时,该商品的inventory.quantity变成了负数! (当然我们不能这样),我该如何解决这个问题,以便当库存表中的项目数量为 0 时,buylist 表不能接受另一个表示有人购买该项目的元组(可能是一个返回错误的函数消息什么的)

在此先感谢,我还是个新手,所以我非常感谢你们的任何帮助和指导。

【问题讨论】:

  • 那你想做什么?计算列、触发器还是选择语句?
  • 可能触发?实际上我不太确定,所以基本上,当我在 buyList 表中插入一个新元组时(意味着有人在买东西),该东西的数量会根据购买的东西自动减去。如果我没有解释好真的很抱歉

标签: sql postgresql sql-update


【解决方案1】:

你需要一个触发器..

    CREATE FUNCTION trigger() RETURNS trigger AS $$ 
    BEGIN 
    UPDATE inventory SET quantity =  NEW.quantity WHERE inventory.item_id = NEW.item_id;
    RETURN NEW;
    END; 
    $$ LANGUAGE plpgsql;

DO $$
DECLARE 
BEGIN
EXECUTE format('CREATE TRIGGER trigger BEFORE INSERT OR UPDATE ON buylist FOR EACH ROW  WHEN (pg_trigger_depth() = 0) EXECUTE PROCEDURE trigger()');
 END;
$$ LANGUAGE plpgsql;


Sample data:

postgres=# select * from inventory;
 item_id | name | quantity | price 
---------+------+----------+-------
       1 | a    |       44 | 10000
       2 | b    |       12 | 12000
       3 | c    |       11 |  5000
       4 | d    |        6 |  3000
(4 rows)

postgres=# select * from buylist;
 buy_id | item_id | quantity 
--------+---------+----------
      1 |       2 |        4
      2 |       2 |        5
      3 |       1 |        1
(3 rows)

postgres=# update buylist set quantity=4 where item_id=1;

postgres=# select * from inventory;
 item_id | name | quantity | price 
---------+------+----------+-------
       2 | b    |       12 | 12000
       3 | c    |       11 |  5000
       4 | d    |        6 |  3000
       1 | a    |       40 | 10000

希望对你有帮助

【讨论】:

  • 非常感谢,你真的帮了我,但我刚刚意识到另一个问题,我稍微编辑了我的问题。希望您能再次提供帮助
猜你喜欢
  • 2010-12-17
  • 2019-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 2022-11-10
  • 2011-11-25
  • 1970-01-01
相关资源
最近更新 更多