我有一个代码,在下面,用户可以把过敏列表。我希望我的代码列出一个食谱列表,从用户过敏列表中排除这些成分。我编写了一个测试代码,但无法找出如何同时排除所有三种成分
userallergy = conn.execute ('SELECT Allergies from User where userid = 4')
userallergy = userallergy.fetchall()
userallergy = userallergy[0][0].replace(" ","")
listallergy=list(userallergy.split(","))
listallergy = ["'%" + i.capitalize() + "%'" for i in listallergy]
print([listallergy])
query='SELECT RecipeName FROM Recipess where Ingredients Not LIKE {}'.format(listallergy[1])
print(query)
aller = conn.execute(query)
saferecipe = aller.fetchall()
print(saferecipe)
您可以使用MySQL REGEX“或”来排除这三个选项。然后,您的查询应该如下所示:
使用
REGEXP
作为一种干净的方法(尽管您不能直接使用它,请查看Problem with regexp python and sqlite的答案):一种简单的方法是通过
AND
操作符连接所有语句(但正如@alexis在评论中提到的,它有自己的问题):相关问题 更多 >
编程相关推荐