SQL:如何获取与表的时间戳字段具有相同月份和年份的条目?

时间:2016-08-14 00:13:34

标签: java mysql sql date timestamp

我有下表

+--------------------------------------+---------------------+---------------------+----------+-----------------------------+----------+--------+
| id                                   | app_start           | app_end             | title    | body                        | location | allDay |
+--------------------------------------+---------------------+---------------------+----------+-----------------------------+----------+--------+
| 52e782d7-8da8-4b27-a17e-d6beb72649e8 | 2004-04-27 10:30:00 | 2005-02-18 11:00:00 | 10 years | Something.                  | NY,USA   |      0 |
| a3e11a10-26d3-407d-83b3-c3cd6cda128f | 2004-02-17 10:30:00 | 2004-02-17 11:00:00 | 12 years | Insert random message here  | NY,USA   |      0 |
+--------------------------------------+---------------------+---------------------+----------+-----------------------------+----------+--------+

此处app_start列的类型为timestamp。如果我想获得特定月份和年份的行,比如2004年第2行(2月)将返回第二行,我会在where子句中插入什么?

 SELECT * FROM table WHERE [????]

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

只需使用monthyear

select *
from yourtable
where month(app_start) = 2 and year(app_start) = 2004
相关问题