5

データベースに次の3つのテーブルがあり、必要な結果をクエリするのに問題があります。材料でレシピを検索しようとしています。

以下のスキーマのSQLフィドル: フィドル

これが私のテーブルです:材料

+---------------+---------+
| ingredient_id | name    |
+---------------+---------+
|             1 | tomato  |
|             2 | onion   |
|             3 | rice    |
|             4 | chicken |
|             5 | beef    |
|             6 | noodles |
|             7 | salt    |
+---------------+---------+

レシピ

+-----------+------------------+
| recipe_id | name             |
+-----------+------------------+
|         1 | tomato goodness  |
|         2 | meat deluxe      |
|         3 | chicken surprise |
+-----------+------------------+

Ingredient_Index

+-----------+---------------+
| recipe_id | ingredient_id |
+-----------+---------------+
|         1 |             1 |
|         1 |             5 |
|         1 |             7 |
|         2 |             5 |
|         2 |             6 |
|         2 |             7 |
|         3 |             4 |
|         3 |             3 |
|         3 |             7 |
+-----------+---------------+

1つの成分のみを検索するクエリは正常に機能し、次のように出力されます。

mysql> select r.recipe_id, r.name
    -> from recipes r
    -> inner join ingredient_index
    -> on i.recipe_id = r.recipe_id
    -> where
    -> i.ingredient_id = 7;

+-----------+------------------+
| recipe_id | name             |
+-----------+------------------+
|         1 | tomato goodness  |
|         2 | meat deluxe      |
|         3 | chicken surprise |
+-----------+------------------+

しかし、複数の材料を使用する場合、または複数の材料に使用する場合、これが得られます

mysql> select r.name
    -> from recipes r
    -> inner join ingredient_index i
    -> on i.recipe_id = r.recipe_id
    -> where i.ingredient_id = 7 or i.ingredient_id = 5;

+------------------+
| name             |
+------------------+
| tomato goodness  |
| tomato goodness  |
| meat deluxe      |
| meat deluxe      |
| chicken surprise |
+------------------+

セット内の5行(0.00秒)

「and」の結果を何も使用せずに使用する

    mysql>  select r.name
    ->  from recipes r
    ->  inner join ingredient_index i
    ->  on i.recipe_id = r.recipe_id
    ->  where i.ingredient_id = 7 and i.ingredient_id = 5;
Empty set (0.00 sec)

どんな助けでも大歓迎です!

4

2 に答える 2

9

レシピは複数の材料を使用でき、指定された1つ以上の材料を使用するレシピを探しているためDISTINCT、レシピが指定されたリストの複数の材料を使用している場合に重複する結果を防ぐために、キーワードを使用する必要があります。また、IN句を使用して、複数の成分IDでフィルタリングすることもできます。

select DISTINCT r.name
from 
    recipes r
    inner join ingredient_index i
    on i.recipe_id = r.recipe_id
where i.ingredient_id IN (7, 5);

または、リストで指定されたすべての材料を使用しているレシピを探している場合は、レシピ名で結果をグループ化し、レコードの数がリストの材料の数と同じであるかどうかを確認できます。

select r.name
from 
    recipes r
    inner join ingredient_index i
    on i.recipe_id = r.recipe_id
where i.ingredient_id IN (7, 5)
GROUP BY r.name
HAVING COUNT(*) = 2

これは、同じ(recipe_id、component_id)タプルを持つ重複レコードがないことを前提としています(UNIQUE制約でより確実になります)。

于 2012-11-17T03:38:06.840 に答える
0

次のフィドル

このクエリ:

select distinct recipe.name
from recipes recipe, ingredient_index i
where i.ingredient_id = 7 or i.ingredient_id = 5;

次の結果セットが生成されます。

NAME  
tomato goodness  
meat deluxe  
chicken surprise  
于 2012-11-17T03:46:04.993 に答える