使用来自 table 的新值更新数组
Update array with new values from table
我有 table 一些商品的价格:
|----------------------------|
| ItemID (INT) | Price (INT) |
|--------------|-------------|
| 1 | 50 |
|--------------|-------------|
| 2 | 36 |
|--------------|-------------|
| 3 | 11 |
|--------------|-------------|
| 4 | 22 |
|--------------|-------------|
| 5 | 54 |
|--------------|-------------|
| 6 | 38 |
|--------------|-------------|
| 7 | 2 |
|--------------|-------------|
| 8 | 1 |
|--------------|-------------|
| 9 | 39 |
|----------------------------|
此 table 包含大部分实际价格,每小时更新一次(例如)。
我有类似 "carts" 的东西,其中包含购物车创建时的实际商品和价格:
|-------------------------------------------------------|
| 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$
因此,我坚持创建新的价格数组。价格的位置应与数组中项目的位置相关联。如何制作?或者更聪明的节省价格的解决方案?
此查询 select carts
价格来自 table items
:
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)
我有 table 一些商品的价格:
|----------------------------|
| ItemID (INT) | Price (INT) |
|--------------|-------------|
| 1 | 50 |
|--------------|-------------|
| 2 | 36 |
|--------------|-------------|
| 3 | 11 |
|--------------|-------------|
| 4 | 22 |
|--------------|-------------|
| 5 | 54 |
|--------------|-------------|
| 6 | 38 |
|--------------|-------------|
| 7 | 2 |
|--------------|-------------|
| 8 | 1 |
|--------------|-------------|
| 9 | 39 |
|----------------------------|
此 table 包含大部分实际价格,每小时更新一次(例如)。
我有类似 "carts" 的东西,其中包含购物车创建时的实际商品和价格:
|-------------------------------------------------------|
| 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$
因此,我坚持创建新的价格数组。价格的位置应与数组中项目的位置相关联。如何制作?或者更聪明的节省价格的解决方案?
此查询 select carts
价格来自 table items
:
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)