说我有这样一张桌子:
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;
结果集是
| id | price | colors | sizes
1| 1 | 100 | {red, blue, green} | {s, l}
1| 3 | 300 | {red} | {s, l, xl}
我的问题是:如何更改查询不仅返回此结果集,还返回此查询的所有可能大小(不包括任何大小条件,即排除sizes&&ARRAY['s', 'l', 'xxl']
),此查询的所有可能颜色(仅排除颜色条件,即不包括colors&&ARRAY['red']
)和价格范围,仅忽略查询中的价格条件(即排除price > 50 AND price < 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}
p_sizes
是查询的不同大小列表:
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}
min_price
和max_price
是查询的价格范围:
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以澄清问题。
答案 0 :(得分:2)
此查询选择预期值:
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)
您可以在横向联接中将查询结果与原始查询结合起来:
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)
您可以将聚合查询拆分为三个部分,以具有不同的where子句,例如:
TabLayout tabLayout = .... (findview or code creation )
/** get selected tab index */
int selectedTabPosition = tabLayout.getSelectedTabPosition();
/** get tab for selected index or if u want any other tab set desired index */
TabLayout.Tab tabAt = tabLayout.getTabAt(selectedTabPosition);
/** get view - but first u need set custom view on tabl via Tab.setCustomView(View) */
View tabView = tabAt.getCustomView():