简体   繁体   中英

How to fetch mysql data between one month to another month

I am looking for query which fetch data between two months.

Like 1 January to 1 May or 1 June to 1 September etc

Thanks, Ali

SELECT * FROM yourTable WHERE dateColumn >= "2016-01-01" AND dateColumn <= "2016-06-01";

你可以很容易地在谷歌上找到这个。

If you're working with a TIMESTAMP column you need to be careful about the end of your selected time interval. Let's say you want everything in the first five months of 2016: 1-Jan-2016 -- 31-May-2016. Then you must select all the timestamps starting with midnight on the first day of the range, up until but not including midnight on the day after the range. That looks like this

SELECT * 
  FROM table
 WHERE timestampColumn >= '2016-01-01'
   AND timestampColumn <  '2016-06-01'

Notice the < on the end-date condition.

Why? The timestamp constant 2016-05-31 actually means 2016-05-31 00:00:00 : or precisely midnight at the beginning of the day in question. So, an entry in your timestampColumn of, for example 2016-05-31 08:45:00 will not be matched by

   timestampColumn <= '2016-05-31'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM