sql - 如何创建虚拟列并在连接后填充数组?

标签 sql postgresql join pg

我有3张 table

表 1 landing_modules

  • l_id
  • 我的名字
  • l_模块
  • l_变体
  • l_version

表 2 类别

  • c_id
  • 姓名

表 3 category_landing_modules

  • l_id
  • c_id

l_idc_id 是从 table_3table_1table_2 的外键分别。现在需要的输出是

landing_modules 中的所有行,这将是

SELECT * FROM  landing_modules;

但另外我想要结果集中的一列作为 category_ids,它应该包含来自 table_3 - category_landing_modules 的相应 c_ids(category_ids)

结果集看起来像

  • l_id
  • 我的名字
  • l_模块
  • l_变体
  • l_版本
  • c_ids

最佳答案

您可以使用相关子查询。

SELECT lm.*,
       ARRAY(SELECT clm.c_id
                    FROM category_landing_modules clm
                    WHERE clm.l_id = lm.l_id) c_ids
       FROM landing_modules lm;

关于sql - 如何创建虚拟列并在连接后填充数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57217089/

相关文章:

MySQL从两个表中选择语句

MySQL : Select the rows having the max value for one field and keep correct values for joining tables

php - SQL inner join 将整数转换为文本

sql - 如何在单个更新查询中更新 json 字段中的多个嵌套键?

sql - 从 JSON 数组中删除对象

postgresql - 如何使用 psql 处理大型结果集?

postgresql - '用户 "postgres"的密码验证失败'

sql - 如何从 Postgres 表中获取数据(多对多)

mysql - SQL 中的多个 LEFT JOIN 运行缓慢。我该如何优化它?

mysql - 在 mysql : how can I select the most recently added row when selecting by MAX if two values are equal (application is a games high score table)