2015-10-25 52 views
0

我有表,價格有些項目:更新陣列用新值從表

|----------------------------| 
| 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$ 

所以,我被困在新的價格陣列創建。價格的位置應該與排列中的項目的位置相關聯。如何做到這一點?或者也許更聰明的解決方案來節省價格?

回答

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; 

查詢必須是如果順序有點複雜數組元素必須保留。在元素聚合的階段,另外使用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)  
+0

但是,正如你所看到的,我有購物車中的每件商品的價格作爲數組,我想用價格表中的價格更新價格。 –

+0

我想我一定誤會了,對不起。見編輯的答案。 – klin

+0

有什麼辦法保持數組中的項目的原始順序? –