mysql的between and 时间
时间: 2023-12-07 15:06:14 浏览: 74
mysql取得两个时间之间的差值
5星 · 资源好评率100%
在MySQL中,使用`BETWEEN`和`AND`关键字进行时间范围查询。下面是使用`BETWEEN`进行时间范围查询的几个示例:
示例1:
```sql
SELECT * FROM tbl_student_info WHERE `height` BETWEEN 1113 AND 1122;
```
等同于:
```sql
SELECT * FROM tbl_student_info WHERE `height` >= 1113 AND `height` <= 1122;
```
示例2:
```sql
SELECT * FROM tbl_student_info WHERE `createDate` BETWEEN '20170101020304' AND '20180101020304';
```
等同于:
```sql
SELECT * FROM tbl_student_info WHERE `createDate` >= '20170101020304' AND `createDate` <= '20180101020304';
```
另外,还可以使用`<if>`条件判断来设定开始时间和结束时间进行查询:
```sql
<if test="endTime!=null and !"".equals(endTime.trim())">
AND endTime < #{endTime}
</if>
<if test="startTime!=null and !"".equals(startTime.trim())">
AND startTime > #{startTime}
</if>
```
需要注意的是,在MySQL中,默认情况下日期查询的时间范围是从`00:00:00`开始计算的。因此,在使用`BETWEEN`进行日期范围查询时,实际上是包含了起始日期的`00:00:00`时刻,但不包含结束日期的`00:00:00`时刻。如果有时间为`00:00:00`的情况,则会包括在查询范围内。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
阅读全文