如何使用查询 array_agg 制作此 sql?
How to make this sql with query array_agg?
我要查询
select * from projects where user_id = 3;
并根据结果 r
,我需要进行 n
查询,其中 n
是 长度 l
的 r
。例如:
| id | project_name | description | user_id |
| 1 | Project A | lorem ipsu | 3 |
| 4 | Project B | lorem ipsu | 3 |
l => 2
然后:
select * from images where project_id = 1;
select * from images where project_id = 4;
好的,如果 l
太大了,你可以看到这是怎么回事。选择太多,对数据库的访问太多。有没有更好的方法来达到这样的最终结果:
| id | project_name | description | user_id | images |
| 1 | Project A | lorem ipsu | 3 | {imgX,imgY,imgZ} |
| 4 | Project B | lorem ipsu | 3 | {imgA,imgB} |
我听说了 postgres 上的 array_agg
函数。也许这就是答案?无论如何,这些是我的 table 描述:
Table "public.projects"
Column | Type | Modifiers
-------------+--------------------------+-------------------------------------------------------
id | integer | not null default nextval('projects_id_seq'::regclass)
name | character varying(255) |
description | character varying(255) |
user_id | integer |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
Table "public.images"
Column | Type | Modifiers
------------+--------------------------+-----------------------------------------------------
id | integer | not null default nextval('images_id_seq'::regclass)
name | character varying(255) |
url | character varying(255) |
project_id | integer |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
提前谢谢你:D
可能对您来说最简单的解决方案是子select。这最接近您之前提到的个别 SELECT
陈述:
SELECT * FROM images
WHERE project_id IN (
SELECT project_id FROM projects
WHERE user_id = 3);
您想要 projects
中的记录加上 image names as array
中匹配 project_id
的记录:
SELECT *
FROM projects
LEFT JOIN LATERAL (SELECT array_agg(name) AS images FROM images WHERE project_id = projects.project_id) x ON true
WHERE user_id = '3'
array_agg
类似于任何其他聚合函数(计数、求和),但 returns 是一个数组而不是标量值。只需连接和分组 2 个表即可实现您的需要。
SELECT p.id, p.name, p.description, p.user_id, array_agg(i.name) images
FROM projects p
LEFT JOIN images i ON p.id = i.project_id
GROUP BY p.id, p.name, p.description, p.user_id
我要查询
select * from projects where user_id = 3;
并根据结果 r
,我需要进行 n
查询,其中 n
是 长度 l
的 r
。例如:
| id | project_name | description | user_id |
| 1 | Project A | lorem ipsu | 3 |
| 4 | Project B | lorem ipsu | 3 |
l => 2
然后:
select * from images where project_id = 1;
select * from images where project_id = 4;
好的,如果 l
太大了,你可以看到这是怎么回事。选择太多,对数据库的访问太多。有没有更好的方法来达到这样的最终结果:
| id | project_name | description | user_id | images |
| 1 | Project A | lorem ipsu | 3 | {imgX,imgY,imgZ} |
| 4 | Project B | lorem ipsu | 3 | {imgA,imgB} |
我听说了 postgres 上的 array_agg
函数。也许这就是答案?无论如何,这些是我的 table 描述:
Table "public.projects"
Column | Type | Modifiers
-------------+--------------------------+-------------------------------------------------------
id | integer | not null default nextval('projects_id_seq'::regclass)
name | character varying(255) |
description | character varying(255) |
user_id | integer |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
Table "public.images"
Column | Type | Modifiers
------------+--------------------------+-----------------------------------------------------
id | integer | not null default nextval('images_id_seq'::regclass)
name | character varying(255) |
url | character varying(255) |
project_id | integer |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
提前谢谢你:D
可能对您来说最简单的解决方案是子select。这最接近您之前提到的个别 SELECT
陈述:
SELECT * FROM images
WHERE project_id IN (
SELECT project_id FROM projects
WHERE user_id = 3);
您想要 projects
中的记录加上 image names as array
中匹配 project_id
的记录:
SELECT *
FROM projects
LEFT JOIN LATERAL (SELECT array_agg(name) AS images FROM images WHERE project_id = projects.project_id) x ON true
WHERE user_id = '3'
array_agg
类似于任何其他聚合函数(计数、求和),但 returns 是一个数组而不是标量值。只需连接和分组 2 个表即可实现您的需要。
SELECT p.id, p.name, p.description, p.user_id, array_agg(i.name) images
FROM projects p
LEFT JOIN images i ON p.id = i.project_id
GROUP BY p.id, p.name, p.description, p.user_id