Sql 如何在不使用联接的情况下在数组_agg中使用子查询

Sql 如何在不使用联接的情况下在数组_agg中使用子查询,sql,postgresql,Sql,Postgresql,我有一个查询,我想写,而不使用join并从另一个表中将所有电话号码合并在一起。 电话号码通过ink_id字段链接在一起 我尝试了这些查询,但它们都给了我一个语法错误 select ink.fullname, array_agg(select phone_number from phone_numbers where phone_numbers.ink_id = ink.id) as _phones from ink 或 墨水台 +-----+------------+

我有一个查询,我想写,而不使用join并从另一个表中将所有电话号码合并在一起。 电话号码通过ink_id字段链接在一起

我尝试了这些查询,但它们都给了我一个语法错误

select
    ink.fullname,
    array_agg(select phone_number from phone_numbers where phone_numbers.ink_id = ink.id) as _phones
from
    ink

墨水台

+-----+------------+
| id  |  fullname  |
+-----+------------+
| 567 | John Smith |
| 159 | Caleb Doe  |
| 333 | Bill Gates |
+-----+------------+
电话号码表

+----+--------+--------------+
| id | ink_id | phone_number |
+----+--------+--------------+
|  1 |    333 |    516519899 |
|  2 |    159 |    216584989 |
|  3 |    333 |    123149849 |
+----+--------+--------------+
所以结果应该是

+-----+------------+----------------------+
| id  |  fullname  |    _phone_numbers    |
+-----+------------+----------------------+
| 567 | John Smith |                      |
| 159 | Caleb Doe  | 216584989            |
| 333 | Bill Gates | 516519899, 123149849 |
+-----+------------+----------------------+

如果使用了正确的语法,第二种方法可以工作:

select ink.fullname,
       (select array_agg(pn.phone_number)
        from phone_numbers pn
        where pn.ink_id = ink.id
       ) as _phones
from ink;

子查询需要用括号括起来。

连接(左)有什么问题吗?我想不用连接,但用子查询代替。子查询需要括号<代码>(选择…@jarlh谢谢,成功了!
select ink.fullname,
       (select array_agg(pn.phone_number)
        from phone_numbers pn
        where pn.ink_id = ink.id
       ) as _phones
from ink;