mysql BETWEEN
时间: 2023-10-15 19:25:04 浏览: 82
MySQL中的BETWEEN是一个操作符,用于选取介于两个值之间的数据范围。这两个值可以是数值、文本或者日期。
举例来说,如果我们有一张名为t_income的数据库表,其中包含了收入金额(amount)和目标收入(target_amount)等字段。我们可以使用BETWEEN操作符来查询满足一定条件的数据。
例如,要查询表中amount大于等于10并且小于等于50的数据,可以使用以下SQL语句:
SELECT * FROM t_income WHERE amount BETWEEN 10 AND 50;
如果要查询表中amount和target_amount的总和大于等于100并且小于等于500的数据,可以使用以下SQL语句:
SELECT * FROM t_income WHERE (amount + target_amount) BETWEEN 100 AND 500;
如果要查询表中create_time在2019-01-01到2019-09-01这个日期范围内的数据,可以使用以下SQL语句:
SELECT * FROM t_income WHERE create_time BETWEEN CAST('2019-01-01' AS DATE) AND CAST('2019-09-01' AS DATE);
另外,如果要查询表中amount小于10或者amount大于50的数据,可以使用以下SQL语句:
SELECT * FROM t_income WHERE amount NOT BETWEEN 10 AND 50;
综上所述,BETWEEN操作符可以在MySQL中用于进行范围查询,根据给定的条件选取相应的数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Mysql中BETWEEN的用法](https://blog.csdn.net/weixin_44901808/article/details/126765629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [mysql中between的用法](https://blog.csdn.net/zhangzehai2234/article/details/102756107)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文