我需要的查询字符串能够找到2个值,由用户插入他们的名字。
每个值中应该有一个在Ingredient1和Ingredient2中(不能同时存在)。如何在sql查询中做到这一点呢?
这就是我到目前为止所尝试的
public static List<IngredientModel> LoadSearchRecipes(string ingredientCategory, string ingredientCategory1)
{
// select ingredient category to show results
string sql = @"SELECT * FROM dbo.Recipe WHERE Ingredient1 IN('" + ingredientCategory.ToString() + "," + ingredientCategory.ToString() + "') AND WHERE Ingredient1 IN('" + ingredientCategory.ToString() + "," + ingredientCategory.ToString() + "');";
return SqlDataAccess.LoadData<IngredientModel>(sql);
}发布于 2020-06-06 01:19:08
每个值中的一个应该在Ingredient1和Ingredient2中(不能同时存在)
在MySQL中,您可以将这两个条件都作为整数进行计算,并确保总和仅为1
where (ingredient1 = ?) + (ingredient2 = ?) = 1实际上,我怀疑您没有使用MySQL。以下是其他数据库的标准方法:
where
(case when ingredient1 = ? then 1 else 0 end)
+ (case when ingredient2 = ? then 1 else 0 end) = 1问号应替换为参数的值(绑定参数的语法可能因数据库而异)。
https://stackoverflow.com/questions/62220770
复制相似问题