PostgreSQL-返回查询结果和不同的可能值
假设我有一张这样的桌子:PostgreSQL-返回查询结果和不同的可能值,postgresql,Postgresql,假设我有一张这样的桌子: CREATE TABLE item ( id text NOT NULL, price integer NOT NULL, colors text[] DEFAULT ARRAY[]::text[], sizes text[] DEFAULT ARRAY[]::text[] ); INSERT INTO item VALUES ('1', 100, array['red', 'blue', 'green'], array['s', 'l
CREATE TABLE item (
id text NOT NULL,
price integer NOT NULL,
colors text[] DEFAULT ARRAY[]::text[],
sizes text[] DEFAULT ARRAY[]::text[]
);
INSERT INTO item VALUES ('1', 100, array['red', 'blue', 'green'], array['s', 'l']);
INSERT INTO item VALUES ('2', 5000, array['yellow', 'green'], array['s']);
INSERT INTO item VALUES ('3', 300, array['red'], array['s', 'l', 'xl']);
INSERT INTO item VALUES ('4', 150, array['white'], array['xxl']);
数据如下:
CREATE TABLE item (
id text NOT NULL,
price integer NOT NULL,
colors text[] DEFAULT ARRAY[]::text[],
sizes text[] DEFAULT ARRAY[]::text[]
);
INSERT INTO item VALUES ('1', 100, array['red', 'blue', 'green'], array['s', 'l']);
INSERT INTO item VALUES ('2', 5000, array['yellow', 'green'], array['s']);
INSERT INTO item VALUES ('3', 300, array['red'], array['s', 'l', 'xl']);
INSERT INTO item VALUES ('4', 150, array['white'], array['xxl']);
现在假设我有这个查询:
SELECT * from item WHERE price > 50 AND price < 400 AND colors&&ARRAY['red'] AND sizes&&ARRAY['s', 'l', 'xxl'] ORDER BY price OFFSET 0 LIMIT 10;
我的问题是:如何更改查询以不仅返回此结果集,还返回此查询的所有可能大小(不包括任何大小条件),即不包括大小和数组['s','l','xxl'],此查询的所有可能颜色(不包括颜色条件),即不包括颜色和数组['red']价格范围只忽略查询中的价格条件,即不包括价格>50和价格<400?
比如:
| id | price | colors | sizes | p_sizes | p_colors | min_price | max_price |
1| 1 | 100 | {red, blue, green} | {s, l} | {s, l, xl} | {'red', 'blue', 'green', 'white'} | 100 | 300 |
1| 3 | 300 | {red} | {s, l, xl} | {s, l, xl} | {'red', 'blue', 'green', 'white'} | 100 | 300 |
其中p_colors是查询的不同颜色列表:
SELECT * from item WHERE price > 50 AND price < 400 AND sizes&&ARRAY['s', 'l', 'xxl'] ORDER BY price OFFSET 0 LIMIT 10;
| id | price | colors | sizes
1| 1 | 100 | {red, blue, green} | {s, l}
1| 4 | 150 | {white} | {xxl}
1| 3 | 300 | {red} | {s, l, xl}
SELECT * from item WHERE price > 50 AND price < 400 AND colors&&ARRAY['red'] ORDER BY price OFFSET 0 LIMIT 10;
| id | price | colors | sizes
1| 1 | 100 | {red, blue, green} | {s, l}
1| 3 | 300 | {red} | {s, l, xl}
SELECT * from item WHERE colors&&ARRAY['red'] AND sizes&&ARRAY['s', 'l', 'xxl'] ORDER BY price OFFSET 0 LIMIT 10;
| id | price | colors | sizes
1| 1 | 100 | {red, blue, green} | {s, l}
1| 3 | 300 | {red} | {s, l, xl}
最小价格和最大价格是查询的价格范围:
SELECT * from item WHERE price > 50 AND price < 400 AND sizes&&ARRAY['s', 'l', 'xxl'] ORDER BY price OFFSET 0 LIMIT 10;
| id | price | colors | sizes
1| 1 | 100 | {red, blue, green} | {s, l}
1| 4 | 150 | {white} | {xxl}
1| 3 | 300 | {red} | {s, l, xl}
SELECT * from item WHERE price > 50 AND price < 400 AND colors&&ARRAY['red'] ORDER BY price OFFSET 0 LIMIT 10;
| id | price | colors | sizes
1| 1 | 100 | {red, blue, green} | {s, l}
1| 3 | 300 | {red} | {s, l, xl}
SELECT * from item WHERE colors&&ARRAY['red'] AND sizes&&ARRAY['s', 'l', 'xxl'] ORDER BY price OFFSET 0 LIMIT 10;
| id | price | colors | sizes
1| 1 | 100 | {red, blue, green} | {s, l}
1| 3 | 300 | {red} | {s, l, xl}
---编辑---
添加了尺寸条件,并将id为2的商品的价格更改为5000以澄清问题。此查询选择预期值:
select
array(select distinct unnest(colors) from item) all_colors,
array(select distinct unnest(sizes) from item) all_sizes,
min(price) min_price,
max(price) max_price
from item;
all_colors | all_sizes | min_price | max_price
-------------------------------+--------------+-----------+-----------
{yellow,blue,white,green,red} | {xl,s,l,xxl} | 100 | 300
(1 row)
您可以在横向联接中将查询结果与原始查询联接:
select *
from item,
lateral (
select
array(select distinct unnest(colors) from item) all_colors,
array(select distinct unnest(sizes) from item) all_sizes,
min(price) min_price,
max(price) max_price
from item
) sub
where price > 50 and price < 400 AND colors&&ARRAY['red'] order by price;
id | price | colors | sizes | all_colors | all_sizes | min_price | max_price
----+-------+------------------+----------+-------------------------------+--------------+-----------+-----------
1 | 100 | {red,blue,green} | {s,l} | {yellow,blue,white,green,red} | {xl,s,l,xxl} | 100 | 300
3 | 300 | {red} | {s,l,xl} | {yellow,blue,white,green,red} | {xl,s,l,xxl} | 100 | 300
(2 rows)
您可以将聚合查询拆分为三个部分,以具有不同的where子句,例如:
select *
from item,
lateral (
select array(
select distinct unnest(colors)
from item
where price > 50 and price < 400) all_colors) q1,
lateral (
select array(
select distinct unnest(sizes)
from item
where price > 50 and price < 400 and colors && ARRAY['red']) all_sizes) q2,
lateral (
select
min(price) min_price,
max(price) max_price
from item
where colors&&ARRAY['red']
) q3
where price > 50 and price < 400 and colors&&ARRAY['red'] order by price;
id | price | colors | sizes | all_colors | all_sizes | min_price | max_price
----+-------+------------------+----------+-------------------------------+-----------+-----------+-----------
1 | 100 | {red,blue,green} | {s,l} | {yellow,white,green,red,blue} | {l,s,xl} | 100 | 300
3 | 300 | {red} | {s,l,xl} | {yellow,white,green,red,blue} | {l,s,xl} | 100 | 300
(2 rows)
这适用于需要表中所有可能值的情况,但我需要限制原始查询结果的可能性,修改为只忽略我需要的可能性所在的列。假设id为2的项目的价格为1000。因为它不是红色的,所以不会被选中,最大价格应该是300,而不是1000。这是否意味着我需要对每个字段进行新的查询?我不明白。如果您只想查询IDs1和ids3,那么所有的颜色都应该是{green,red,blue},但是问题中有{'red',blue',green',yellow',white',因为我不想只查询IDs1和ids3。要获得所有的颜色,我需要进行相同的查询,但没有颜色和数组['red']条件。这使得我们的价格只有>50和<400。在这种情况下,每件商品的价格恰好都在这个范围内。也许这是个坏例子。我将用更多的数据编辑这个问题。