荟萃馆

位置:首页 > 计算机 > 计算机二级

2017计算机二级MySQL考试在MySQL数据库比较日期的方法

引导语:你知道如何进行MySQL数据库比较日期吗,下面就让本站小编给大家带来这个知识点吧,欢迎阅读学习!

2017计算机二级MySQL考试在MySQL数据库比较日期的方法

在在MySQL数据库中比较日期的方法

String hql = "from MaintainInfo where begDate>=’1992-11-24’ and endDate <=’2009-11-02’";

mysql数据库中如何对日期字段转换为相应天数

举例:格式为’2007-12-2516:50:00’

SELECT * FROM `cn_web` WHERE TO_DAYS(NOW()) - TO_DAYS(`date`) <= 13; 这样查询出来cn_web表中13天内的数据。

今天碰到要在hql选取符合时间段的数据,时间比较成了问题,经过上网搜索及实践,总结如下:

String begindate = arameter("begindate");

String enddate = arameter("enddate");

//以上两数据从页面得到,为string类型

hql = " from a as model";

hql += " where d=’"+"556655" + "’";

if(!ank(begindate)){

hql += " and time>=to_date(’ " + begindate +"’,’yyyy-MM-dd HH:mi:ss’)";

}

if(!ank(enddate)){

hql += " and time<=to_date(’ " + enddate +"’,’yyyy-MM-dd HH:mi:ss’)";

}

或者也可以直接用between ... to ...