13

内部結合に基づいて一部の結果を除外するクエリを作成する方法を理解しようとしています。

次のデータを検討してください。

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 で、component_id が 1 または 2 の formula_batch_component を持つすべての formula_batch レコードを選択したいので、次のクエリを実行します。

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"

このクエリを変更して、基準に一致する一意の Formula_batch レコードのみを取得する方法はありますか?

例えば:

1;"F1.1"
2;"F1.2"
4;"F1.all"

御時間ありがとうございます!

4

3 に答える 3