定义和用法
EXTRACT()
函数用于返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。
语法
EXTRACT(unit FROM date)
date 参数是合法的日期表达式。unit 参数可以是下列的值:
Unit 值 |
---|
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 |
EXTRACT() 示例
提取年份
select extract(year from now())
提取第几周
select extract(weeks from now())
提取微秒计时
select extract(second_microsecond from now())
从字段中提取相应的时间单位:
SELECT sum(order_amount), EXTRACT(week FROM create_date) AS week,
FROM orders
group by week
where date(create_date)="2012"
讨论区