0

i.e

Suppose the current date is 18/10/2016 then i want to fetch all record from 18/10/206 to 30/11/2016

This is the scenario, please help me to get effective way to fetch this from in a single query mysql.

Jigar7521
  • 1,549
  • 14
  • 27

2 Answers2

3

You can use BETWEEN to compare dates.

Take help of LAST_DAY(date) function.

SELECT 
* 
FROM your_table 
WHERE your_date BETWEEN CURDATE() AND LAST_DAY(CURDATE() + INTERVAL 1 MONTH)

Note:

LAST_DAY() function

MySQL LAST_DAY() returns the last day of the corresponding month for a date or datetime value. If the date or datetime value is invalid, the function returns NULL.

1000111
  • 13,169
  • 2
  • 28
  • 37
  • Sorry to say but this is giving a syntax error – Jigar7521 Oct 18 '16 at 07:41
  • Please share the query you tried along with the error you encountered. – 1000111 Oct 18 '16 at 07:45
  • This is the error : SQL query: Documentation SELECT * FROM members_detail Birthdate BETWEEN CURDATE() AND LAST_DAY(CURDATE() + INTERVAL 1 MONTH) LIMIT 0, 25 MySQL said: Documentation #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BETWEEN CURDATE() AND LAST_DAY(CURDATE() + INTERVAL 1 MONTH) LIMIT 0, 25' at line 1 – Jigar7521 Oct 18 '16 at 07:49
  • 1
    Sorry there are missing a where keyword, and that was of my mistake – Jigar7521 Oct 18 '16 at 07:50
  • I see you didn't use `WHERE` – 1000111 Oct 18 '16 at 07:51
1
$time = strtotime(date('Y-m-d'));
$final = date("Y-m-d", strtotime("+1 month", $time));
$nextMonth= date('t/m/Y',strtotime($final));
$now=date('d/m/Y');
$sql="select * from table where date>='".$now.' and date<=$nextMonth";
$res=mysql_query($sql);
Pradyut Manna
  • 588
  • 1
  • 3
  • 12