简体   繁体   English

获取当月的数据

[英]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.所以我需要使用 sql 请求从几个月的最后几天获取数据。 For example :例如 :

  1. Now date is 22/08/2016 , I need to get data from 01/08/2016-22/08/2016 ;现在日期是22/08/2016 ,我需要从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 ;现在日期是04/06/2016 ,我需要从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.DATE_FORMAT(NOW() ,'%Y-%m-01')将返回您当前月份和年份的第一个日期。

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();你正在寻找这样的东西: 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)给你(对于curdate()在这里)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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