【发布时间】:2013-07-31 07:45:31
【问题描述】:
我正在尝试了解如何创建查询以根据内部联接过滤掉一些结果。
考虑以下数据:
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
我想选择 project_id 为 1 的所有 Formulation_batch 记录,并且有一个 component_id 为 1 或 2 的 Formulation_batch_component。所以我运行以下查询:
SELECT formulation_batch.*
FROM formulation_batch
INNER JOIN formulation_batch_component
ON formulation_batch.id = formulation_batch_component.formulation_batch_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"
有没有办法修改这个查询,以便我只取回符合条件的唯一的formulation_batch 记录?
EG:
1;"F1.1"
2;"F1.2"
4;"F1.all"
感谢您的宝贵时间!
【问题讨论】:
标签: sql postgresql inner-join