如何使用内部联接查询防止重复(Postgres)

drk*_*101 7 sql postgresql inner-join

我试图了解如何创建一个查询,以根据内部联接筛选出一些结果.

请考虑以下数据:

formulation_batch
-----
id  project_id  name    
1   1           F1.1
2   1           F1.2
3   1           F1.3
4   1           F1.all

formulation_batch_component
-----
id  formulation_batch_id    component_id
1   1                       1
2   2                       2
3   3                       3
4   4                       1
5   4                       2
6   4                       3
7   4                       4
Run Code Online (Sandbox Code Playgroud)

我想选择project_id为1的所有formula_batch记录,并且有一个component_batch_component,其component_id为1或2.所以我运行以下查询:

SELECT "formulation_batch".* 
FROM "formulation_batch" 
INNER JOIN "formulation_batch_component" 
ON "formulation_batch"."id" = "formulation_batch_component"."formulationBatch_id" 
WHERE "formulation_batch"."project_id" = 1 
    AND (("formulation_batch_component"."component_id" = 2 
        OR "formulation_batch_component"."component_id" = 1 ))
Run Code Online (Sandbox Code Playgroud)

但是,这会返回一个重复的条目:

1;"F1.1"
2;"F1.2"
4;"F1.all"
4;"F1.all"
Run Code Online (Sandbox Code Playgroud)

有没有办法修改此查询,以便我只返回符合条件的唯一formula_batch记录?

例如:

1;"F1.1"
2;"F1.2"
4;"F1.all"
Run Code Online (Sandbox Code Playgroud)

谢谢你的时间!

Clo*_*eto 15

在这种情况下,可以distinctjoin可能使其更高性能之前应用它:

select fb.* 
from
    formulation_batch fb
    inner join
    (
        select distinct formulationbatch_id
        from formulation_batch_component
        where component_id in (1, 2)
    ) fbc on fb.id = fbc.formulationbatch_id 
where fb.project_id = 1
Run Code Online (Sandbox Code Playgroud)

请注意如何使用表名的别名来使查询更清晰.此外,in操作员非常方便.不必使用带有这些标识符的双引号.


Gor*_*off 9

一种方法是使用distinct:

SELECT distinct "formulation_batch".* 
FROM "formulation_batch" 
INNER JOIN "formulation_batch_component" 
ON "formulation_batch"."id" = "formulation_batch_component"."formulationBatch_id" 
WHERE "formulation_batch"."project_id" = 1 
    AND (("formulation_batch_component"."component_id" = 2 
        OR "formulation_batch_component"."component_id" = 1 ))
Run Code Online (Sandbox Code Playgroud)