【发布时间】:2021-09-03 10:54:51
【问题描述】:
我有 3 个主表:products、stores 和 categories
-
产品通过表 product_store
与 stores 进行多对多关联 -
产品通过表 product_category
与 categories 进行多对多关联
我如何查询所有产品,属于一家商店(使用 store_id : 1)并属于一个或多个类别(使用 [4,5,6] 中的 category_id?我使用了以下语法,但结果有重复的产品记录(对于例如,第 5 类和第 6 类的产品将显示 2 次)。我可以让它独一无二吗?
select
"products"."name"
from
"products"
inner join "product_categories" as "categories_join" on "categories_join"."product_id" = "products"."id"
inner join "categories" on "categories_join"."category_id" = "categories"."id"
inner join "product_stores" as "stores_join" on "stores_join"."product_id" = "products"."id"
inner join "stores" on "stores_join"."store_id" = "stores"."id"
where
"categories"."id" in ( 4,5,6,7 )
and "stores"."id" = 1
【问题讨论】:
-
只需
SELECT DISTINCT。 -
谢谢,这么简单