我想知道如何創建一個查詢,以篩選出基於內加入了一些成果。如何防止內部聯接查詢(Postgres的)重複
考慮以下數據:
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
我想用1 PROJECT_ID選擇所有formulation_batch記錄,與1或2。所以我運行下面的查詢COMPONENT_ID一個formulation_batch_component:
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))
然而,這將返回一個重複的條目:
1;"F1.1"
2;"F1.2"
4;"F1.all"
4;"F1.all"
是否有方法可以修改此查詢,以便我只返回符合條件的唯一公式製品記錄記錄?
EG:
1;"F1.1"
2;"F1.2"
4;"F1.all"
感謝您的時間!
謝謝,我忘了是不同的。它並不是最明智的表現,但仍然比在軟件層面進行過濾要好。 – drkstr1