-3

I need to Select three month's values from my database except the first day values of the third month.

For example:

I want to select values for three months since now: May, April, March without values from 1 March

Every day of month has 24 hour values for temperature and i want to exclude the first day of the third month like i said before.

Thank you!!

  • In each month you have one record in database (except March, where you have two records)? If not, which records you want to select? And I hope you store date in `date(time)` type column. You can add your attemps, what have you tried so far. – pavel May 31 '15 at 17:39
  • 5
    Please edit your question with sample data, desired results, and a query you have tried. A SQL Fiddle is also really helpful. – Gordon Linoff May 31 '15 at 17:39
  • thank you for your time! – Alex Vasile Jun 01 '15 at 12:46

3 Answers3

1

Get the first day of the month(in three months) - How to get first day of every corresponding month in mysql?

CAST(DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 2 MONTH) ,'%Y-%m-01') as DATE)

get your query between dates:

date BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 3 MONTH)

and your query should be like:

WHERE date BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 3 MONTH) AND date != CAST(DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 2 MONTH) ,'%Y-%m-01') as DATE)
Community
  • 1
  • 1
Shalev Shalit
  • 1,945
  • 4
  • 24
  • 34
0
SELECT * 
FROM tablename 
WHERE 
   (date LIKE '____-05-__' 
 OR date LIKE '____-04-__' 
 OR date LIKE '____-03-__') 
AND date NOT LIKE '____-03-01';
shA.t
  • 16,580
  • 5
  • 54
  • 111
Varun Garg
  • 2,464
  • 23
  • 37
0

Use DAY and MONTH function with YEAR to filter your dates like:

YEAR(date) = YEAR(CURDATE()) 
AND 
( MONTH(date) > MONTH(CURDATE()) - 2
  OR 
  ( MONTH(date) = MONTH(CURDATE()) - 2
    AND
     DAY(date) > 1 ))
shA.t
  • 16,580
  • 5
  • 54
  • 111