Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
POSTGRESQL:如何连接数组列上的表?_Postgresql - Fatal编程技术网

POSTGRESQL:如何连接数组列上的表?

POSTGRESQL:如何连接数组列上的表?,postgresql,Postgresql,我想请您帮助创建一个postgresql查询,这样我就可以离开连接类别和产品表,并用实际的产品名称替换prodcutnums select c.name, array_agg(p.name) as products from categories c left join products p on p.id = any(c.productnums) group by c.name; 下面可以看到查询的表结构和所需输出 类别表: name

我想请您帮助创建一个postgresql查询,这样我就可以离开连接类别和产品表,并用实际的产品名称替换prodcutnums

select c.name, 
       array_agg(p.name) as products
from categories c 
   left join products p on p.id = any(c.productnums)
group by c.name;
下面可以看到查询的表结构和所需输出

类别表:

              name               |              productnums                                 
---------------------------------+------------------------------
 Books                           | {605,614,663,647,645,619,627}
 Kitchen                         | {345,328}
 Electronics                     | {145,146}
产品表:

              id                 |              name                                 
---------------------------------+----------------------
 145                             | LCD Monitor
 147                             | Mouse
 345                             | Glass
期望输出:

              name               |              productnums                                 
---------------------------------+-------------------------------------------
 Electronics                     | {LCD Monitor,Mouse}
我非常感谢您的支持。

您可以在连接条件中使用any运算符,然后使用array\u agg聚合产品名称

select c.name, 
       array_agg(p.name) as products
from categories c 
   left join products p on p.id = any(c.productnums)
group by c.name;