首页 > 解决方案 > 在真子句上左侧连接 Postgresql 没有得到它

问题描述

努力理解左加入横向

begin;
create temp table manufacturers(id serial primary key, name text);
create temp table get_product_names (pid integer, id integer, product text, 
    CONSTRAINT fk_id FOREIGN KEY(id)   REFERENCES manufacturers(id) );
insert into manufacturers(name) values('m1'),('m2'),('m3'),('m4'),('m5');
insert into get_product_names(pid,id, product) values(1,1,'typea');
insert into get_product_names(pid,id, product) values(2,2,'typeb');
insert into get_product_names(pid,id, product) values(3,4,'typec');
commit;

从手册中引用

LEFT JOIN 到 LATERAL 子查询通常特别方便,因此即使 LATERAL 子查询没有为它们生成任何行,源行也会出现在结果中。例如,如果 get_product_names() 返回制造商生产的产品名称,但我们表中的一些制造商当前没有生产产品,我们可以找出哪些是这样的:

SELECT m.name
FROM manufacturers m LEFT JOIN LATERAL get_product_names(m.id) pname ON true
WHERE pname IS NULL;

但是当我执行代码时:发生以下错误。

No function matches the given name and argument types. You might need to add explicit type casts.

那么如何在真子句上复制左侧连接。或者我的代码哪里出错了?

标签: postgresqllateral-join

解决方案


你没有创建一个名为 get_product_names(int) 的函数。

您不能横向连接到裸表名称。它必须是子查询,是函数。


推荐阅读