psql 9.1将select结果合并到一行

时间:2015-12-09 22:14:12

标签: select psql

我有4个表,每个表都有超过10K的行:

                         Table "public.items"
 Column         |           Type   |   Modifiers                      
----------------+------------------+---------------
 id             | integer          | not null default nextval       
 title          | text             | not null   

           Table "public.issues"
Column       |   Type          | Modifiers                                
-------------+-----------------+------------------
id           | integer          | not null default nextval
name         | text             | not null  

 Table "public.authors"
  Column     |        Type          |     Modifiers                       
--------------+---------------------+-----------------------
id           | integer              | not null default  nextval
first_name   | text                 | not null
last_name    | text                 | not null

Table "public.item_issues"
  Column  |  Type   | Modifiers 
----------+---------+-----------
 item_id  | integer | not null
 issue_id | integer | not null


Table "public.item_authors"
 Column   |  Type   | Modifiers 
 -----------+---------+-----------
item_id   | integer | not null
author_id | integer | not null

我想列出所有项目及其作者和问题,知道某个项目可能有多个问题和多个作者。到目前为止,我的选择查询返回正确的信息,但在几行。

查询:

select i.id, i.title, iss.id  as issue_id, iss.name, a.first_name, a.last_name
join item_authors ia on (ia.item_id = i.id) 
join authors a on (a.id = ia.author_id) 
join issues iss on (iss.id = iis.issue_id)
join item_issues iis on (iis.item_id = i.id)   

结果:

id  | title | issue_id | name    | first_name | last_name       
----|-------|----------|---------|------------|--------------   
 1  |  A    |  22      | elected | David      | Lowes  
 1  |  A    |  54      | health  | David      | Lowes 
 2  |  C    |  10      | art     | Adam       | Dole 
 2  |  C    |  10      | art     | John       | Paul 
 3  |  D    |  28      | eat     | Barbara    | Wu  
 3  |  D    |  17      | weight  | Barbara    | Wu  
 3  |  D    |  54      | health  | Barbara    | Wu  
 3  |  D    |  28      | eat     | Sam        | Sara 
 3  |  D    |  17      | weight  | Sam        | Sara 
 3  |  D    |  54      | health  | Adam       | Dole  
 3  |  D    |  28      | eat     | Adam       | Dole  
 3  |  D    |  17      | weight  | Adam       | Dole 
 3  |  D    |  54      | health  | Sam        | Sara    

但我想:

id  | title | issue_id   |       name          | first_name last_name 
----|-------|------------|---------------------|--------------------------
 1  |  A    | 22,54      | elected, health     | David Lowes 
 2  |  C    | 10         | art                 | John Paul, Adam Dole 
 3  |  D    | 28, 17, 54 | eat, weight, health | Barbara Wu, Sam Sara, Adam Dole   

是否可以在一个查询中使用?

1 个答案:

答案 0 :(得分:0)

使用array_aggarray_to_string函数组合,您可以执行以下操作。

select i.id, i.title, 
  array_to_string(array_agg(iss.id), ', ') as issue_id, 
  array_to_string(array_agg(iss.name), ', ') as name, 
  array_to_string(array_agg(a.first_name || a.last_name), ', ') as "first_name last_name"
from issues i
join item_authors ia on ia.item_id = i.id
join authors a on a.id = ia.author_id
join issues iss on iss.id = iis.issue_id
join item_issues iis on iis.item_id = i.id
group by i.id, i.title