MySQL:在同一列上有 AND 条件

标签 mysql

请问同一列是否可以有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           |
+----------------------------------------------------------+

问题是我只需要返回传统 Adob​​o,因为我希望我的 WHERE 查询能够准确地了解食谱的成分。

非常感谢您的帮助,谢谢!

最佳答案

正如我在评论中所说,你想要的是关系代数中的除法。实际上有很多方法可以做到这一点(您可以在网络上进行搜索)。

这是我发现做你想做的事情的方法之一:

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:我没有检查查询,但想法就在那里。

关于MySQL:在同一列上有 AND 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28125656/

相关文章:

python - 在共享服务器 : No module named MySQLdb? 上安装 Django

查询中的 MySQL NOW() + 14 或 CURDATE() + 14

MySQL 可选存储过程参数

php - 涉水通过另一个 "programmer' s"PHP/MySQL 代码

mysql - 带 Pivot 的存储过程不返回结果结构,而是返回零值

MySQL - 创建定义器语法错误

PHP 显示来自 MYSQL 的文本

mysql - 从与给定类别 ID 关联的所有子类别级别中选择列表

Mysql查询获取表名存储在另一个表中的每个表的总和

mysql - MySql YEAR 类型的正确 Doctrine 列类型是什么