子查询返回多于一行

more than one row returned by subquery

我有一个查询,我正在使用合并到 select 其中一个值。但是,我收到一条错误消息,指出用作表达式的子查询返回了不止一行。我正在使用 Postgres v9.3。 我的代码:

 select input1,
 input 2,
 (select (coalesce(tblstuff2.input4, '') || coalesce(tblstuff.input5, ''))
  from tblstuff
  join tblstuff2 on....
  where ...)
input 6,
from..
where...

我需要合并以从 table 添加两列(字符串)并显示为一列。对于 ex:if 列,输入 4 是手机号码,输入 5 是 phone ,至少其中一个应该出现。

至少有三种解法:

不同

select distinct (coalesce(tblstuff2.input4, '') || coalesce(tblstuff.input5, ''))
  from tblstuff
  join tblstuff2 on....
  where ...

如果 join return 相同 values.If 不一样,这不起作用。

限制 1

select (coalesce(tblstuff2.input4, '') || coalesce(tblstuff.input5, ''))
  from tblstuff
  join tblstuff2 on....
  where ...
  limit 1

如果 join return 有不同的值,但您丢失了其中一个。

string_agg

select string_agg(coalesce(tblstuff2.input4, '') || coalesce(tblstuff.input5, ''), ', ')
  from tblstuff
  join tblstuff2 on....
  where ...

如果 join return 的值不同并且您想要 return 那么全部。