简体   繁体   中英

Get data from the current month

I have a question. So I need to get data from the last days of months using a sql request. For example :

  1. Now date is 22/08/2016 , I need to get data from 01/08/2016-22/08/2016 ;
  2. Now date is 04/06/2016 , I need to get data from 01/06/2016-04/06/2016 ;

Sorry but I dont have an idea. Thx in advance and sorry for my english

I would do something like this:

SELECT <table-columns> FROM <table-name>
WHERE (<date-column> BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )

This DATE_FORMAT(NOW() ,'%Y-%m-01') will return the first date of your current month and year.

Need to use something like:

YEAR(time) = YEAR(NOW())
AND MONTH(time) = MONTH(NOW())

You are looking for something like this: SELECT * FROM tbl_name WHERE yourDate.MONTH()=NOW().MONTH();

Refine the syntax from here .

You need to get the first day of the month for a given date then, in order to make your comparison with the database:

select date(concat_ws('-',year(curdate()),lpad(month(curdate()),2,'00'),'01')); gives you that (for curdate() here)

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