gpt4 book ai didi

SQL Server 2008,如何检查数据库中是否存在多条记录?

转载 作者:行者123 更新时间:2023-12-04 18:11:24 25 4
gpt4 key购买 nike

我有 3 张 table :

  • 食谱:
  • 身份证、姓名
  • 成分:
  • 身份证、姓名
  • 配方成分:
  • id、recipeId、componentId、数量

  • 每次客户创建新配方时,我都需要查看 recipeingredient表来验证这个配方是否存在。如果 ingredientIdquantity完全一样,我会告诉客户配方已经存在。由于我需要检查多行,因此需要帮助来编写此查询。

    最佳答案

    了解您的成分和数量后,您可以执行以下操作:

    select recipeId as ExistingRecipeID
    from recipeingredient
    where (ingredientId = 1 and quantity = 1)
    or (ingredientId = 8 and quantity = 1)
    or (ingredientId = 13 and quantity = 1)
    group by recipeId
    having count(*) = 3 --must match # of ingeredients in WHERE clause

    关于SQL Server 2008,如何检查数据库中是否存在多条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12585550/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com