2017計算機二級MySQL考試在MySQL數據庫比較日期的方法

來源:文萃谷 2.49W

引導語:你知道如何進行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 ...

熱門標籤