Select between dates in Mysql

Asked

Viewed 971 times

5

Good afternoon,

I have the following problem:

I have a column DATA_ENTRADA date type and need a select to compare the DATA_ENTRADA and the "current date" ( ex: today is 28/08/2015, tomorrow would be 29/08/2015 ), and return me only the records with the data_input >= 90 days

If anyone can help me thank you, I’ve tried everything but without success.

  • everything I searched referred me to between, but I can only get queries between specific dates, and what I need is dates > 90 days, comparing the input date and the current date.

  • 1

    Someone tested my answer, because only I didn’t win +1 ? it hurts.... if I could vote on my own answer I would vote...

  • @mayconrocha you want from the current date now() kurdate() the records that are 90 days after or 90 days earlier because it has 2 responses p/ each interpretation

4 answers

8

Simply calculate the difference between the desired field and the current date (now) in the Where.

As it is not quite clear what you want, then follow the two possible solutions:

90 days prior to "today"

SELECT * FROM dados WHERE DATEDIFF(now(), data_entrada) >= 90

See working on Sqlfiddle.

90 days after "today"

SELECT * FROM dados WHERE DATEDIFF(data_entrada, now()) >= 90

See working on Sqlfiddle.

Simply change the order of the parameters of the DATEDIFF that solves both problems.

  • 1

    I don’t understand why the negative, what’s the problem with the answer?

  • 2

    I will change again p/down, your Insert are all with date less than 90 days from today so could not bring results, so I understood the question.

  • I read back the question and it’s really ambiguous.

  • Then I get my down even, your Fiddle is giving return May 1 and April 25, and on meo would have to return only dates forward of August 28 which is the now() or Kurdish() or such current date

  • I edited with the two possible answers @Sneepsninja

  • 1

    rsrsrs although you do not enter in Fiddle records with date greater than 90 days later... I will give the +1 because SQL itself is correct, both p/ more as p/ less

Show 1 more comment

6

WHERE DATE_SUB(data_atual,INTERVAL 90 DAY) >= data_entrada

This returns only dates that are 90 days or more before the current date

  • your query also does not return results follow http://sqlfiddle.com/#test! 9/8d38e/3 should return 2 records as tested, the AP either 90 after the current date kurdate() or now() as I understand it, if it is 90 previous to the current date I will revert ok

5

select * from suaTabela where data_entrada >= DATE_ADD(curdate(),INTERVAL 90 DAY);

Explanation: DATE_ADD( param1, INTERVAL 90 DAY ), where param1 would be the current date and the second parameter would be the amount of days that in the case is 90.

I made a Fork Luis Henrique to explain the divergence in SQL Fiddle as far as I understood from the now() or curdate() which would be current date the records longer than 90 days.

  • +1 for the fiddle.

0

Have you tried using the CURDATE() + INTERVAL 90 DAY?

  • 1

    Hello Gustavo! Edit your reply to look more like a reply than a comment :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.