sql - 如何在 Postgres 中连接多个表?

标签 sql postgresql

我有5张 table ,

product: 
id | name | description | enable
category:
id | name | enable
category_product: 
id | product_id | category_id
image: 
id | name | files | enable
image_product:
id | product_id | image_id

我希望我的连接表类似于这样:

产品名称 |产品描述 |类别名称 |图片名称 |图片.文件

最佳答案

我猜您想知道如何连接没有直接 关系但通过其他表的表?那么答案就是在所有必要的表上构建一个连接链。

SELECT p.name,
       p.description,
       c.name,
       i.name,
       i.file
       FROM product p
            INNER JOIN product_category pc
                       ON pc.product_id = p.id
            INNER JOIN category c
                       ON c.id = pc.category_id
            INNER JOIN image_category ic
                       ON ic.product_id = p.id
            INNER JOIN image i
                       ON i.id = ic.image_id;

关于sql - 如何在 Postgres 中连接多个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55621477/

相关文章:

sql - T-SQL 选择第一个实例

sql - mysql 显示 group by 语句的空值

sql - 为 MYSQL 记录获取合理随 secret 钥的最佳方法

postgresql - 如何在 PostgreSQL 中声明并使用临时表作为游标源?

postgresql - 在 PostgreSQL 中为临时变量分配特定数据类型

java - 在 Spring 启动一段时间后,数据库连接丢失

mysql - 数据库的最新结果

SQL只选择没有重复的记录

ruby-on-rails - 列 "users.id"必须出现在 GROUP BY 子句中或用于聚合函数中

performance - J2EE - PostgreSQL - JDBC - 慢速查询