【问题标题】:Update array with new values from table使用表中的新值更新数组
【发布时间】:2015-10-25 14:52:36
【问题描述】:

我有一些商品的价格表:

|----------------------------|
| ItemID (INT) | Price (INT) |
|--------------|-------------|
|       1      |      50     |
|--------------|-------------|
|       2      |      36     |
|--------------|-------------|
|       3      |      11     |
|--------------|-------------|
|       4      |      22     |
|--------------|-------------|
|       5      |      54     |
|--------------|-------------|
|       6      |      38     |
|--------------|-------------|
|       7      |       2     |
|--------------|-------------|
|       8      |       1     |
|--------------|-------------|
|       9      |      39     |
|----------------------------|

此表包含大多数实际价格,每小时更新一次(例如)。

我有类似“购物车”之类的东西,其中包含在购物车创建时的实际商品和价格:

|-------------------------------------------------------|
| CartID (INT) | Items (INTEGER[]) | Prices (INTEGER[]) |
|--------------|-------------------|--------------------|
|       1      |      {1,2,3}      |     {50,25,310}    |
|--------------|-------------------|--------------------|
|       2      |      {4,5,6}      |    {1337,20,32}    |
|--------------|-------------------|--------------------|
|       3      |     {1,9,6,7}     |   {258,356,711,2}  |
|-------------------------------------------------------|

在某些情况下,我必须将此购物车中的价格更新为实际价格。

我的功能(未完成):

CREATE OR REPLACE FUNCTION public.prices_update(startindex integer)
 RETURNS void
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$
DECLARE
    cart RECORD;
    prices INTEGER[];
    t RECORD;
BEGIN
    FOR bet IN
        SELECT * FROM Carts WHERE CartID > startindex
    LOOP
        prices := ARRAY[]::INTEGER[];
        FOR t IN
            SELECT ItemID, Price FROM Prices WHERE ItemID = ANY(cart.Items)
        LOOP
            prices := prices || t.Price;
        END LOOP;
        RAISE NOTICE '%', prices;
    END LOOP;
END;
$function$

所以,我坚持创建新的价格数组。价格位置应与数组中项目的位置相关联。怎么做?或者更聪明的节省价格的解决方案?

【问题讨论】:

    标签: postgresql plpgsql postgresql-9.4


    【解决方案1】:

    此查询从表 items 中选择 carts 的价格:

    select cartid, array_agg(c.itemid) items, array_agg(i.price) prices
    from (
        select cartid, unnest(items) itemid
        from carts
        where cartid > 0        -- startindex
        ) c
        join items i using(itemid)
    group by 1
    order by 1;
    
     cartid |   items   |    prices    
    --------+-----------+--------------
          1 | {1,2,3}   | {50,36,11}
          2 | {4,5,6}   | {22,54,38}
          3 | {1,6,7,9} | {50,38,2,39}
    (3 rows)
    

    使用上面的查询更新carts

    update carts c
    set items = n.items, prices = n.prices
    from (
        select cartid, array_agg(c.itemid) items, array_agg(i.price) prices
        from (
            select cartid, unnest(items) itemid
            from carts
            where cartid > 0        -- startindex
            ) c
            join items i using(itemid)
        group by 1
        order by 1
        ) n
    where n.cartid = c.cartid;
    

    如果必须保留数组元素的顺序,则查询必须稍微复杂一些。在元素聚合的阶段额外使用 order by row_number()。

    select cartid, array_agg(itemid) items, array_agg(price) prices
    from (
        select cartid, n, c.itemid, i.price
        from (
            select cartid, itemid, row_number() over (partition by cartid) n
            from (
                select cartid, unnest(items) itemid
                from carts
                where cartid > 0        -- startindex
                ) c
            ) c
            join items i using(itemid)
        order by 1, 2
        ) c
    group by 1
    order by 1;
    
     cartid |   items   |    prices    
    --------+-----------+--------------
          1 | {1,2,3}   | {50,36,11}
          2 | {4,5,6}   | {22,54,38}
          3 | {1,9,6,7} | {50,39,38,2}
    (3 rows)    
    

    【讨论】:

    • 但是如您所见,我将购物车中每个商品的价格作为数组,我想用价格表中的价格更新数组中的价格。
    • 我想我一定是误会了,抱歉。请参阅编辑后的答案。
    • 有没有办法保持数组中项目的原始顺序?
    猜你喜欢
    • 2021-07-08
    • 2021-05-18
    • 2021-02-18
    • 1970-01-01
    • 2022-09-27
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    相关资源
    最近更新 更多