我想打一个查询如何使查询array_agg与此SQL?
select * from projects where user_id = 3;
,并根据它的结果r
,我需要做n
查询,其中n
是长度的r
l
。例如:
| 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 "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
解释询问太 –
对不起,我认为这很容易理解。编辑。 –