如何 select SQL 中的顶级架构?
How to select top level schema in SQL?
我在顶层有一个 table foo
,在架构 bar
.
中有一个 table foo
使用此查询时:
select *
from bar.foo
natural join foo
where foo.baz = 'test'
我收到错误:
ERROR: table reference "foo" is ambiguous
LINE 4: where foo.baz = 'test'
我可以使用别名解决这个问题:
select *
from bar.foo
natural join foo as f
where f.baz = 'test'
但我想知道是否可以不使用别名而直接指定顶级架构(向 SQL 表明我不是在谈论 bar.foo
)?例如这样的事情:
select *
from bar.foo
natural join foo
where .foo.baz = 'test'
或者像这样:
select *
from bar.foo
natural join foo
where root_schema.foo.baz = 'test'
没有 "top-level" 模式这样的东西。所有模式都在同一级别。 优先搜索个不合格对象,由search_path
定义
默认安装中的默认架构称为 public
,其中创建了所有不合格的对象。所以在你的情况下你很可能想使用它。
由于两个 table 同名,您还需要为它们分别使用 table 别名以便能够区分它们。
select *
from bar.foo bf
join public.foo pf on pf.id = bf.id
where bf.baz = 'test'
我在顶层有一个 table foo
,在架构 bar
.
foo
使用此查询时:
select *
from bar.foo
natural join foo
where foo.baz = 'test'
我收到错误:
ERROR: table reference "foo" is ambiguous
LINE 4: where foo.baz = 'test'
我可以使用别名解决这个问题:
select *
from bar.foo
natural join foo as f
where f.baz = 'test'
但我想知道是否可以不使用别名而直接指定顶级架构(向 SQL 表明我不是在谈论 bar.foo
)?例如这样的事情:
select *
from bar.foo
natural join foo
where .foo.baz = 'test'
或者像这样:
select *
from bar.foo
natural join foo
where root_schema.foo.baz = 'test'
没有 "top-level" 模式这样的东西。所有模式都在同一级别。 优先搜索个不合格对象,由search_path
定义默认安装中的默认架构称为 public
,其中创建了所有不合格的对象。所以在你的情况下你很可能想使用它。
由于两个 table 同名,您还需要为它们分别使用 table 别名以便能够区分它们。
select *
from bar.foo bf
join public.foo pf on pf.id = bf.id
where bf.baz = 'test'