我有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/