我有一个查询,我想使用联接写 wihout ,并从另一个表中将所有电话号码合并在一起。 电话号码通过ink_id字段链接在一起。
我尝试了这些查询,但是它们都给了我一个语法错误。
select
ink.fullname,
array_agg(select phone_number from phone_numbers where phone_numbers.ink_id = ink.id) as _phones
from
ink
或
select
ink.fullname,
select(array_agg(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 |
+-----+------------+----------------------+
答案 0 :(得分:1)
如果使用正确的语法,第二种方法将起作用:
select ink.fullname,
(select array_agg(pn.phone_number)
from phone_numbers pn
where pn.ink_id = ink.id
) as _phones
from ink;
子查询需要用括号括起来。