我想问一下,是否可以为同一列提供AND条件?
我需要这样做的原因是为我们的查询提供准确的结果。
样本表:
+=====================================+
| recipe |
+-------------------------------------+
| ID | name | description | servings |
+-------------------------------------+
| 1 | Adobo | N/A | 6 |
+-------------------------------------+
| 2 | S.P.C | N/A | 5 |
+-------------------------------------+
| 3 | Kare2 | N/A | 6 |
+=====================================+
+============================+
| recipe_ingredients |
+----------------------------+
| ID | recipe_id | name |
+----------------------------+
| 1 | 1 | Pork |
+----------------------------+
| 2 | 1 | Soy Sauce |
+----------------------------+
| 3 | 1 | Garlic |
+----------------------------+
| 4 | 2 | Garlic |
+----------------------------+
| 5 | 2 | Onion |
+----------------------------+
| 6 | 3 | Vinegar |
+----------------------------+
某些食谱含有相同的成分,但是当我查询时,我只需要根据我给出的确切成分获得食谱。这是我的代码:
SELECT DISTINCT
tbl_recipe.name, tbl_recipe.description, tbl_recipe.user_name,
tbl_ingredients.ingredient_name, tbl_ingredients.ingredient_measure_type, tbl_ingredients.ingredient_measure_value
FROM recipes AS tbl_recipe
LEFT JOIN recipe_ingredients AS tbl_ingredients
ON tbl_ingredients.recipe_id = tbl_recipe.id
WHERE tbl_ingredients.ingredient_name IN ('Garlic', 'Soy Souce')
返回:
+----------------------------------------------------------+
| name | description | tbl_ingredients.name |
+----------------------------------------------------------+
| Traditional Adobo | N/A | Soy Souce |
+----------------------------------------------------------+
| Traditional Adobo | N/A | Garlic |
+----------------------------------------------------------+
| S.P.C | N/A | Garlic |
+----------------------------------------------------------+
预期输出:
+----------------------------------------------------------+
| name | description | tbl_ingredients.name |
+----------------------------------------------------------+
| Traditional Adobo | N/A | Soy Souce |
+----------------------------------------------------------+
| Traditional Adobo | N/A | Garlic |
+----------------------------------------------------------+
问题是我只需要返回传统阿斗波,因为我希望我的WHERE查询与配方的成分完全相同。
非常感谢您的帮助!
答案 0 :(得分:0)
正如我在评论中所说,你想要的是关系代数中的一个分裂。实际上有很多方法可以做到这一点(你可以在网上进行搜索)。
这是我找到你想要的方式之一:
SELECT tbl_recipe.name
FROM recipes AS tbl_recipe
WHERE NOT EXISTS (
SELECT tbl_ingredients.ingredient_name
FROM recipe_ingredients AS tbl_ingredients
WHERE ingredient_name IN ('Garlic', 'Soy Souce')
AND NOT EXISTS (
SELECT R.name
FROM recipes AS R
LEFT JOIN recipe_ingredients AS I
ON I.recipe_id = R.id
WHERE R.id = tbl_recipe.ID AND I.recipe_id = tbl_recipe.recipe_id));
请注意,查询只会返回所有具有所有这些成分的接收(大蒜和大豆)。您可以操纵查询以返回其他内容,例如食谱的描述......这只是一个想法。 这个查询背后的idia(这样你可以更容易理解)就是你搜索的内容与搜索你想要检查的每个食谱相同,那里没有没有用于该食谱的成分。
P.S:我没有检查查询,但想法就在那里。
答案 1 :(得分:0)
您可以为每种成分多次加入配料表。 假设您将成分放在一个单独的表中,recipe_ingredients是链接表:
SELECT
r.name,
r.description,
CONCAT(i1.name, ',', i2.name) ingredients
FROM
recipe r
JOIN
recipe_ingredients ri
ON r.ID = ri.recipe_id
JOIN
ingredients i1
ON ri.ingredient_id = i1.id AND i1.name = 'Soy sauce'
JOIN
ingredients i2
ON ri.ingredient_id = i2.id AND i2.name = 'Garlic'
LEFT JOIN
ingredients i3
ON ri.ingredient_id = i3.id AND i3.name NOT IN ('Soy sauce', 'Garlic')
WHERE
i3.id IS NULL
使用左连接和i3.id IS NULL条件(反连接),将返回仅包含这两种成分的配方