我有一个基本数据库,有几列我将在下面显示
我有一个程序,允许用户输入是男性还是女性,如果他们想
,则输入NULL然后他们必须选择一个类别,如爱或烹饪
我尝试了这个查询,但它没有工作它显示其他性别忽略Where Gender = M语句
它只能是一个性别但不同类型的分类
任何帮助将不胜感激
SELECT *
FROM products
WHERE Gender = "M"
AND Category = "Love"
OR Category = "Cooking"
数据库
Id Name Gender Category
1 AB M Love
2 AC F Love
3 AS M Cooking
4 SF Null Cooking
答案 0 :(得分:2)
您需要在OR
周围添加括号像
这样的东西SELECT *
FROM products
WHERE Gender = "M"
AND (Category = "Love"
OR Category = "Cooking")
你拥有它的方式,它与
相同SELECT *
FROM products
WHERE (Gender = "M"
AND Category = "Love")
OR Category = "Cooking"
答案 1 :(得分:1)
您必须将类别包装在括号中,以告诉查询性别必须为“M”:
SELECT *
FROM products
WHERE Gender = "M"
AND (Category = "Love"
OR Category = "Cooking")
答案 2 :(得分:0)
我认为你需要在这里使用IN运算符,试试这个:
SELECT *
FROM products
WHERE Gender = 'M'
AND Category IN ('Love','Cooking')