MySQL支援按年月日查詢

2021-09-24 21:39:24 字數 2497 閱讀 5997

在業務環境中,總是想要篩選出不同時間條件下的資料,例如我只想查詢當天資料,當月資料或者本年資料。於是就想通過mysql自帶的幾個時間函式進行簡單的實現。

select

*from

cars_location

where

year

( create_time )

=year

(now()

)and

month

( create_time )

=month

(now()

)and

day( create_time )

=day

(now()

)

2、

select

*from

cars_location

where

date

( create_time )

= curdate(

);

查詢本年:

select

*from

`cars_location`

where

year

( create_time )

=year

(now()

)

查詢本月:

select

*from

`cars_location`

where

year

( create_time )

=year

(now()

)and

month

( create_time )

=month

(now()

)

首先我們來了解,假設我有乙個dateflag屬性,專門用來判斷前端傳過來的查詢時間條件,並且規定dateflag='day』表示查詢當天資料,dateflag='month』表示查詢當月資料,dateflag='year』表示查詢當年資料,以下就是mybatis的實現片段可供參考,其中需要注意在if test中的==條件後的引數需要加單引號。

test

="dateflag != null and dateflag == 'day'

">

and year ( create_time ) = year ( now( ) )

and month ( create_time ) = month ( now( ) )

and day ( create_time ) = day ( now( ) )

if>

test

="dateflag != null and dateflag == 'month'

">

and year ( create_time ) = year ( now( ) )

and month ( create_time ) = month ( now( ) )

if>

test

="dateflag != null and dateflag == 'year'

">

and year ( create_time ) = year ( now( ) )

if>

通過了解mysql的時間函式以及集合mybatis的寫法,成功對接後台實現根據dateflag查詢當日、當月或者本年的資料。題外話:我們從後台返回的時間戳總是毫秒數,如果我們要轉換成"yyyy-mm-dd hh:mm:ss"的格式,那麼可以參考以下的js函式進行轉換:

function

timeformat

(data)

else

var h =

( date.

gethours()

<10?

'0'+

(date.

gethours()

): date.

gethours()

)+':';

var m =

( date.

getminutes()

<10?

'0'+

(date.

getminutes()

): date.

getminutes()

)+':';

var s =

( date.

getseconds()

<10?

'0'+

(date.

getseconds()

): date.

getseconds()

);datatime =y+

m+d+h+m+s;

returny+

m+d+h+m+s;

}return datatime;

}}

nginx日誌定時切割 按年月日

2 宣告乙個獨特的log format並命名 在下面的server location,我們就可以引用 mylog 在server段中,這樣來宣告 nginx允許針對不同的server做不同的log 有的web伺服器不支援,如lighttp access log logs access 8080.lo...

SQL 按年,月,日統計相關函式

說明 統計每月下組織每天費用記錄 表 membermoney 字段 id 主鍵 使用者id varchar 50 onorgid 所在單位 varchar 50 realpay 金額 decimal 18,2 paydate 繳費時間 datetime addyear 年 int paymonth ...

獲取年月日

需求 獲取當前日期的前乙個月份 當月有 31 天時,js 日期物件 setmonth 問題 當前日期如果不是 31 號,是沒問題的,是 31 號就會有問題 比如今天是 2018 09 30 號,前乙個月應該是 2018 08 30 let now new date new date 2018 09 ...