最新的Web开发教程
 

MySQL的EXTRACT() Function


<MySQL的日期函数

定义和用法

EXTRACT()函数是用于返回的日期/时间的单个部件,如年,月,日,时,分,等

Syntax

EXTRACT(unit FROM date)

凡日期是有效的日期表达式和单位可以是以下之一:

Unit Value
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH

假设我们有以下的"Orders"表:

OrderId ProductName OrderDate
1 Jarlsberg Cheese 2014-11-22 13:23:44.657

下面的SELECT语句:

SELECT EXTRACT(YEAR FROM OrderDate) AS OrderYear,
EXTRACT(MONTH FROM OrderDate) AS OrderMonth,
EXTRACT(DAY FROM OrderDate) AS OrderDay
FROM Orders
WHERE OrderId=1

会导致这样的:

OrderYearOrderMonthOrderDay
20141122

<MySQL的日期函数