mysql - 在单个条件中使用多个 AND 的 SQL 查询

标签 mysql sql

我需要一个包含多个 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/

相关文章:

mysql - 关于可空外键和规范化的数据库设计问题

php - 在 MySQL 中设置名称 utf8?

mysql - 如何用行更新 SQL 表?

mysql - 具体外键解释

mysql - 如何生成没有外键的ER图(MySQL)?

mysql - 分区和更新

mysql - 使用条件语句选择

mysql - 给定时间间隔的 SQL 查询?

php - 我如何在mysql中创建案例,仅在空时更新字段,如果不空,则转到下一个字段?

c# - 使用 View ,抽象实际表,使用 View 更新多个表