从日期取年和月 [英] Take year and month from date

查看:47
本文介绍了从日期取年和月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 列的数据库,第一列是日期(日期数据类型).我想从年或月或两者中过滤结果.我发现了这个:

I have a database with 3 columns, the first column is date (date data type).I want to filter results from years or months or both. I found this:

SELECT * FROM Orders WHERE OrderDate='2015-12-11';

但是,例如,我如何选择 2015 年或 2015 年和第 11 个月的所有内容?

But how can I select for example, everything with year 2015 or year 2015 and month 11?

推荐答案

您可以使用 MySQL 的内置过滤结果 日期函数 YEAR()MONTH() 函数:

You can filter results using MySQL's built in date functions YEAR() and MONTH() functions:

SELECT * 
FROM Orders 
WHERE 
YEAR(OrderDate) = 2015 AND 
MONTH(OrderDate) = 11;

您还可以使用这些来返回其他结果:

You can also use these to return additional results:

SELECT YEAR(OrderDate) AS OrderYear FROM Orders

或者您可以对结果进行排序:

Or you can sort your results:

SELECT * AS OrderYear FROM Orders ORDER BY YEAR(OrderDate)

这篇关于从日期取年和月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆