別の方法:
バージョン 2 (ストアド プロシージャとして) 改訂
select r.name
from recipes r
where r.id = (select t1.recipe_id
from RecipeIngredients t1 inner join
RecipeIngredients t2 on t1.recipe_id = t2.recipe_id
and t1.ingredient_id = @recipeId1
and t2.ingredient_id = @recipeId2)
編集 2: [人々が叫び始める前に] :)
これは、バージョン 2 の先頭に配置できます。これにより、ID を渡す代わりに名前でクエリを実行できます。
select @recipeId1 = recipe_id from Ingredients where name = @Ingredient1
select @recipeId2 = recipe_id from Ingredients where name = @Ingredient2
バージョン 2 をテストしましたが、動作します。この場合、Ingredient テーブルにリンクするほとんどのユーザーはまったく必要ありませんでした。
編集 3: (テスト結果);
このストアド プロシージャを実行すると、次の結果が得られます。
結果の形式は次のとおりです (最初の Recipe_id ; 2 番目の Recipe_id, Result)
1,1, Failed
1,2, 'banana cream pie'
1,3, 'chocolate banana surprise'
2,1, 'banana cream pie'
2,2, Failed
2,3, 'chocolate cream pie'
3,1, 'chocolate banana surprise'
3,2, 'chocolate cream pie'
3,3, Failed
明らかに、このクエリは、両方の制約が同じ場合は処理しませんが、他のすべてのケースでは機能します。
編集4:(同じ制約ケースの処理):
この行を置き換えます:
r.id = (select t1...
に
r.id in (select t1...
失敗したケースで動作して、以下を提供します。
1,1, 'banana cream pie' and 'chocolate banana surprise'
2,2, 'chocolate cream pie' and 'banana cream pie'
3,3, 'chocolate cream pie' and 'chocolate banana surprise'