2017計算機二級MySQL考試在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 ...