sql如何获取一列日期的年份和月份

2020-04-22 时尚 82阅读
WHERE
Year( 订购日期 ) = 2004 AND MONTH ( 订购日期 ) = 5;
就可以了。
注: MySQL 中, 获取 日期的组成部分, 有2套函数可以使用, 下面是执行的例子代码:
mysql> SELECT 'NOW' DisplayMode, NOW() B
-> UNION ALL
-> SELECT 'Second' DisplayMode, Second(NOW()) B
-> UNION ALL
-> SELECT 'Minute' DisplayMode, MINUTE(NOW()) B
-> UNION ALL
-> SELECT 'Hour' DisplayMode, Hour(NOW()) B
-> UNION ALL
-> SELECT 'Day' DisplayMode, DAY(NOW()) B
-> UNION ALL
-> SELECT 'Week' DisplayMode, Week(NOW()) B
-> UNION ALL
-> SELECT 'Month' DisplayMode, Month(NOW()) B
-> UNION ALL
-> SELECT 'Year' DisplayMode, Year(NOW()) B;
+-------------+---------------------+
| DisplayMode | B |
+-------------+---------------------+
| NOW | 2013-04-07 09:29:54 |
| Second | 54 |
| Minute | 29 |
| Hour | 9 |
| Day | 7 |
| Week | 14 |
| Month | 4 |
| Year | 2013 |
+-------------+---------------------+
8 rows in set (0.00 sec)
mysql> SELECT '年' AS name, EXTRACT(YEAR FROM NOW()) AS value
-> UNION ALL
-> SELECT '年月' AS name, EXTRACT(YEAR_MONTH FROM NOW()) AS value
-> UNION ALL
-> SELECT '月' AS name, EXTRACT(MONTH FROM NOW()) AS value
-> UNION ALL
-> SELECT '日' AS name, EXTRACT(DAY FROM NOW()) AS value
-> UNION ALL
-> SELECT '时' AS name, EXTRACT(HOUR FROM NOW()) AS value
-> UNION ALL
-> SELECT '分' AS name, EXTRACT(MINUTE FROM NOW()) AS value
-> UNION ALL
-> SELECT '秒' AS name, EXTRACT(SECOND FROM NOW()) AS value;
+------+--------+
| name | value |
+------+--------+
| 年 | 2013 |
| 年月 | 201304 |
| 月 | 4 |
| 日 | 7 |
| 时 | 9 |
| 分 | 27 |
| 秒 | 43 |
+------+--------+
7 rows in set (0.00 sec)
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com