PostgreSQL - 根据其他表

时间:2017-08-26 21:37:44

标签: postgresql sql-delete postgresql-9.6

附上SQLFiddle这个问题。

我有两个表,我想用inventoryfruitsforuser表删除fruitsForPrize

它看起来像这样:

select * from fruitsForPrize where "prizeID" = 1
prizeID|fruitID
-------+-------
1      |1
1      |1
1      |1
1      |1
1      |2
1      |2
1      |3
1      |3
1      |4
1      |4
1      |4
1      |5
1      |5
1      |5

select * from inventoryfruitsforuser where "userID" = 1 
userID |fruitID
-------+-------
1      |1
1      |1
1      |1
1      |2
1      |2
1      |3
1      |4
1      |5
1      |5

现在,userID 1想获得奖品1,因此需要从inventoryfruitsforuser

中删除prizeID 1水果

所以它会像

那样

(inventoryfruitsforuser where userID = 1) - (fruitsForPrize where prizeID = 1)

我会得到一张看起来像这样的表:

inventoryfruitsforuser      
userID |fruitID
-------+-------
1      |1
1      |3
1      |4
1      |4
1      |5

我做了一个查询,检查我是否有足够的水果来获得奖金

with myfruits as (
    select "fruitID", count("fruitID") from inventoryFruitsForUser where "userID" = 1 group by "fruitID" order by "fruitID"
),fruitsRequired as (
    select "fruitID", count("fruitID") from fruitsForPrize where "prizeID" = 1 group by "fruitID" order by "fruitID"
),checkShouldDelete as (
    select fruitsRequired."fruitID" as "fruitsRequired.fruitID", fruitsRequired.count as "fruitsRequired.count", myfruits."fruitID" as "myfruits.fruitID", myfruits.count as "myfruits.count",
           (myfruits.count is not null and myfruits.count >= fruitsRequired.count) or (myfruits.count is null and fruitsRequired.count = 0) as "isEnough"
    from fruitsRequired
    left join myfruits
    on (fruitsRequired."fruitID" = myfruits."fruitID")
) SELECT bool_and("isEnough") "toDelete" FROM checkShouldDelete;

我要做的就是使用inventoryfruitsforuser where "userID" = 1fruitsForPrize where "prizeID" = 1删除行,这样我就会得到第三张表。

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

同意@gwaigh您的架构可以使用一些工作。在任何情况下,您遇到的困难之一是每条记录上没有主键/唯一键,因此删除可能会很困难。这可以很容易地修复(或者您可以按照@gwaigh描述的方式更改模型的工作 - 添加" number_of_pieces"列):

ALTER TABLE fruitsForPrize ADD COLUMN id serial;
ALTER TABLE inventoryfruitsforuser ADD COLUMN id serial;

一旦到位,删除变得更加直截了当。我相信你是"从库存中删除件并兑换奖品",对吗?

(我也认为你的一些列名可能会有一些变化。资本在SQL领域变得艰难)

我解决的方法是枚举你的水果,然后从库存中删除相应的元素。

我不确定如何更新小提琴,所以这里是删除查询(显然可以添加到您之前的CTE字符串中):

with prize_enum as (
     -- enumerate fruit for prize
    select "prizeID","fruitID"
    , id as prizefruitid
    , row_number() over (partition by "prizeID","fruitID" order by id) as fruitnum
    from fruitsForPrize
  )
, inv_enum as (
     -- enumerate the fruit in inventory
    select "userID","fruitID",id as invid
    , row_number() over (partition by "userID","fruitID" order by id) as fruitnum 
     from inventoryfruitsforuser
  )
DELETE FROM inventoryfruitsforuser z
USING
 (
 -- get the fruits we are redeeming for prize
  select *
  from prize_enum p
  join inv_enum i on i."fruitID" = p."fruitID"
    and i.fruitnum = p.fruitnum
) a
where a.invid = z.id
returning *;

select * from inventoryfruitsforuser;
-- returns the table you expect

(注意 - 不解决多个用户或多个奖品的可能性)

编辑: @gwaigh建议的方法。我相应地更改了DDL:

CREATE TABLE inventoryfruitsforuser ("userID" INTEGER NOT NULL, "fruitID" INTEGER NOT NULL, number_of_pieces integer NOT NULL DEFAULT 1);
CREATE TABLE fruitsForPrize ("prizeID" INTEGER NOT NULL, "fruitID" INTEGER NOT NULL, number_of_pieces integer NOT NULL DEFAULT 1);

INSERT INTO inventoryfruitsforuser ("userID", "fruitID",number_of_pieces)
VALUES  (1,1,4),
        (1,2,2),
        (1,3,2),
        (1,4,3), 
        (1,5,3);

INSERT INTO fruitsForPrize ("prizeID", "fruitID",number_of_pieces)
VALUES  (1,1,3),
        (1,2,2), 
        (1,3,1),
        (1,4,1),
        (1,5,2);

ALTER TABLE fruitsForPrize ADD COLUMN id serial;
ALTER TABLE inventoryfruitsforuser ADD COLUMN id serial;

此处更新的查询更容易一些。需要记住的一点是,我没有解决多个奖项(即多个奖品会计算相同的水果,因为在您选择奖品之前,库存不会减少。这可以通过循环奖励来解决,明确声明奖品,或改变查询有点复杂)。希望这说明了这一点:

with check_prize as (
  select p.*, u."userID", u.number_of_pieces >= p.number_of_pieces and u.number_of_pieces is not null as enough
  from fruitsForPrize p
  left join inventoryfruitsforuser u on u."fruitID" = p."fruitID"
  ) , enough_for_prize  as (
select "userID", "prizeID", true = ALL(array_agg(enough)) as enough
from check_prize
group by 1,2
    )
    update inventoryfruitsforuser u 
    set number_of_pieces = u.number_of_pieces - p.number_of_pieces
    from fruitsForPrize p
    join enough_for_prize e on e."prizeID" = p."prizeID" and e.enough
    where e."userID" = u."userID" and p."fruitID" = u."fruitID"
  returning *
    ;

select * from inventoryfruitsforuser;

如果您要专注于特定用户/奖品,我可能会将整个事情包装在存储过程中并使用用户/奖品作为参数......如下:

create or replace function user_redeem_prize(_user integer, _prize integer)
RETURNS BOOLEAN
AS $BODY$
BEGIN

    with check_prize as (
      select p.*, u."userID", u.number_of_pieces >= p.number_of_pieces and u.number_of_pieces is not null as enough
      from fruitsForPrize p
      left join inventoryfruitsforuser u on u."fruitID" = p."fruitID"
      where p."prizeID" = _prize and u."userID" = _user
      ) , enough_for_prize  as (
    select "userID", "prizeID", true = ALL(array_agg(enough)) as enough
    from check_prize
    group by 1,2
        )
        update inventoryfruitsforuser u 
        set number_of_pieces = u.number_of_pieces - p.number_of_pieces
        from fruitsForPrize p
        join enough_for_prize e on e."prizeID" = p."prizeID" and e.enough
        where e."userID" = u."userID" and p."fruitID" = u."fruitID"
        and e."userID" = _user
        and p."prizeID" = _prize
        ;

   RETURN TRUE;
END;
$BODY$
LANGUAGE PLPGSQL;

select user_redeem_prize(1,1);