我需要一个包含多个 AND 的 SQL 查询。让我用一个例子来解释一下,
例如我想在数据库中搜索一个特性,其价格大于1000且小于2000(价格在1000-2000之间),并且其面积大于1000平方英尺。且少于2000平方尺(面积在1000-2000之间)。 所以我猜测查询可能是,
SELECT *
FROM table_name
WHERE (price>1000 AND price<2000) AND (area>1000 AND area<2000)
这就是我需要的东西!谢谢
最佳答案
您的原始查询对我来说看起来不错,但您也可以使用 BETWEEN如果您愿意,请尝试以下操作:
SELECT * FROM table_name WHERE (price BETWEEN 1001 AND 2000) AND (area BETWEEN 1001 AND 2000);
expr BETWEEN min AND max
If expr is greater than or equal to min and expr is less than or equal to max, BETWEEN returns 1, otherwise it returns 0. This is equivalent to the expression (min <= expr AND expr <= max) if all the arguments are of the same type. Otherwise type conversion takes place according to the rules described in Section 12.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments
关于mysql - 在单个条件中使用多个 AND 的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44262325/