1

我正在尝试执行这样的查询...

select * from table1 as t1
left join (
    select * from table2 as t2
    where t2.id = t1.t2_id
) as tt2
where tt2.value = 'SOME VALUE'

&我收到这样的错误...

ERROR: invalid reference to FROM-clause entry for table "t1"
  Hint: There is an entry for table "t1", but it cannot be referenced from this part of the query.

该错误消息完全有道理,但我只想知道是否可以将 't1' 的值与保持相同结构的 't2' 匹配?

4

1 回答 1

0

如果你真的想要一个相关的子查询,你可以使用横向连接:

select *
from table1 t1 left join lateral
     (select * 
      from table2 t2
      where t2.id = t1.t2_id
     ) tt2
     on 1=1
where tt2.value = 'SOME VALUE';

请注意,该where子句撤消左连接。我怀疑你真的想要一个简单的left join

select *
from table1 t1 left join
     table2 t2
     on t2.id = t1.t2_id and t2.value = 'SOME VALUE';

这将返回在witht1中没有匹配行的行。如果您不想要它们,请将 更改为.t2'SOME VALUE'left joininner join

于 2021-04-04T12:34:19.063 回答