MySQL 将查询的日期按年月归档

【知识准备】

1. 使用到的 MySQL 日期函数:

函数 功能
DATE_FORMAT(date, fnt) 返回按字符串 fmt 格式化的日期 date 值

2. 使用到的 MySQL 的日期和时间格式:
格式符 格式说明
%Y 四位数字表示的年份,如:2016、2017、2018...(注意 Y 为大写)
%m 两位数字表示的月份,如:01、02、03...(注意 m 为小写)

例:将当前时间转为年月格式(当前时间为 2016年3月15日23:03:03)

mysql> select DATE_FORMAT(NOW(), '%Y%m'); -- 将当前时间转为年月格式
+----------------------------+
| DATE_FORMAT(NOW(), '%Y%m') |
+----------------------------+
| 201603                     |
+----------------------------+
1 row in set (0.00 sec)


本例中数据库表结构:

mysql> desc blog.blog_article;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| _id               | int(11)      | NO   | PRI | NULL    | auto_increment |
| _content          | text         | NO   |     | NULL    |                |
| _create_time      | datetime     | YES  |     | NULL    |                |
| _is_allow_comment | bit(1)       | YES  |     | b'1'    |                |
| _is_delete        | bit(1)       | YES  |     | b'0'    |                |
| _order            | int(11)      | YES  |     | NULL    |                |
| _title            | varchar(255) | NO   |     | NULL    |                |
| _author           | int(11)      | YES  | MUL | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+
8 rows in set (0.03 sec)


本例中数据库表数据:

mysql> select * from blog.blog_article \G;
*************************** 1. row ***************************
              _id: 1
         _content: <p>这是文章一。</p>
     _create_time: 2016-01-03 13:26:57
_is_allow_comment: 
       _is_delete:
           _order: 0
           _title: 文章一
          _author: 1
*************************** 2. row ***************************
              _id: 2
         _content: <p>这是文章二。</p>
     _create_time: 2016-03-11 19:40:06
_is_allow_comment: 
       _is_delete:
           _order: 0
           _title: 文章二
          _author: 1
*************************** 3. row ***************************
              _id: 3
         _content: <p>这是文章三。</p>
     _create_time: 2016-03-15 20:12:50
_is_allow_comment: 
       _is_delete:
           _order: 0
           _title: 文章三
          _author: 1
3 rows in set (0.00 sec)

【实例】
1. 将数据库表中的 _create_time 字段按年月降序归档;
mysql> select distinct DATE_FORMAT(a._create_time, '%Y%m') from blog.blog_article as a order by a._create_time DESC;
+-------------------------------------+
| DATE_FORMAT(a._create_time, '%Y%m') |
+-------------------------------------+
| 201603                              |
| 201601                              |
+-------------------------------------+
2 rows in set (0.00 sec)


2. 归档时也可以做一点儿格式上的修饰;

mysql> select distinct DATE_FORMAT(a._create_time, '%Y年%m月') from blog.blog_article as a order by a._create_time DESC;
+-------------------------------------------+
| DATE_FORMAT(a._create_time, '%Y年%m月')   |
+-------------------------------------------+
| 2016年03月                                |
| 2016年01月                                |
+-------------------------------------------+
2 rows in set (0.00 sec)


3. 查询 _create_time 在 2016 年 3 月份的数据;

mysql> select * from blog.blog_article as a where DATE_FORMAT(a._create_time, '%Y%m') = '201603' \G;
*************************** 1. row ***************************
              _id: 2
         _content: <p>这是文章二。</p>
     _create_time: 2016-03-11 19:40:06
_is_allow_comment: 
       _is_delete:
           _order: 0
           _title: 文章二
          _author: 1
*************************** 2. row ***************************
              _id: 3
         _content: <p>这是文章三。</p>
     _create_time: 2016-03-15 20:12:50
_is_allow_comment: 
       _is_delete:
           _order: 0
           _title: 文章三
          _author: 1
2 rows in set (0.00 sec)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章