我想打一個查詢如何使查詢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
解釋詢問太 –
對不起,我認爲這很容易理解。編輯。 –