开发者

Get Record of the given month in MySQL?

I have a table like :

id     trans_date                  resp_code 
------------------------------------------------
1      2011-02-15 12:47:09    1 
2      2011-02-24 12:47:09    1
3      2011-02-29 12:47:09    1
4      2011-03-11 12:47:09    1
5      2011-03-15 12:47:09    1
6      2011-03-22 12:47:09    1
7      2011-04-10 12:47:09    1
8      2011-04-11 12:47:09    1

All I want is to fetch all the records of the given month. Like For the month March[03], I should get row 4,5开发者_Python百科, and 6.

Thanks in advance.....


SELECT *
  FROM tbl
 WHERE trans_date BETWEEN '2011-03-01' AND DATE_ADD('2011-03-01', INTERVAL 1 MONTH)

You could also follow this query

SELECT *
  FROM tbl
 WHERE MONTH(trans_date) = 3
   AND YEAR(trans_date) = 2011

but it will cause fullscan thus will be slow on large amount of data


You could use MySQL's MONTH() function

SELECT * FROM tbl WHERE MONTH( trans_date ) = 3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜